Sitemap
Abbas Roholamin

Abbas Roholamin

What is code splitting?

3 min readAug 12, 2024
Press enter or click to view image in full size

Code splitting is a technique in JavaScript (and broadly in web development) that helps optimize the loading time of applications by breaking down the bundle of code into smaller chunks. This allows only the necessary code to be loaded when needed, rather than sending a large bundle all at once. Here’s a breakdown of how it works and its benefits:

Bundling vs. Splitting:

🏭 Bundling: Typically, modern JavaScript applications are bundled together into one or a few large files to reduce the number of HTTP requests. However, this can lead to long initial load times because the browser has to download the entire bundle.

🍽 Code Splitting: Code splitting allows you to split your code into smaller pieces (chunks) that can be loaded on demand. This means that not all code is loaded upfront, which can significantly improve the performance of an application.

🔃 Dynamic Import:

Code splitting can be achieved using import() (dynamic imports) which create a split point in your code. The corresponding chunk is loaded at runtime when the dynamic import is encountered.

// Example of dynamic import
const module = await import('./myModule.js');

🤔 Types of Code Splitting:

👉 Entry Points: You can define multiple entry points in your application. Each entry point can produce a separate bundle, which will be loaded when the application requires a specific entry.

🗂 Route-Based Splitting: In applications that use a front-end framework (such as React, Vue, or Angular), code splitting can be implemented at the routing level. This means that a specific route will load its associated code only when the user navigates to that route.

💧 Component-Based Splitting: Specific components within an application can be loaded only when needed. For example, a modal component can be imported dynamically when the user clicks a button to open it.

🌲 Benefits of Code Splitting

Get Abbas Roholamin’s stories in your inbox

Join Medium for free to get updates from this writer.

✈ Improved Initial Load Time: Users can start interacting with the application faster as only the initial code needed for the application to run is loaded.

↘ Reduced Load on Browser: By decreasing the amount of code that needs to be parsed and executed at startup, it can lead to better performance, especially on low-powered devices.

💽 Optimized Resource Loading: Resources are only loaded when needed, which can save bandwidth and increase efficiency.

👤 User Experience: Users benefit from faster interaction times and a smoother experience without long waiting periods.

Tools and Frameworks

Webpack: A popular module bundler for JavaScript that supports code splitting out of the box. You can configure it using various options (entry, optimization.splitChunks, etc.).

React: React provides built-in support for code splitting with React.lazy() and Suspense.

Vue: Vue supports code splitting through dynamic imports and its built-in routing system.

Example with Webpack

Here’s a basic example of how you might set up code splitting with Webpack:

// In Webpack configuration
module.exports = {
//…
optimization: {
splitChunks: {
chunks: 'all',
},
},
};
// In your application code
const button = document.getElementById('loadModule');
button.addEventListener('click', () => {
import('./path/to/yourModule').then(module => {
const yourFunction = module.default;
yourFunction();
});
});

In this example, when the user clicks the button, the yourModule is loaded dynamically.

Summary

Code splitting is a powerful strategy for optimizing JavaScript applications. By loading code on demand and breaking down large files into manageable chunks, developers can improve the performance and user experience of their applications significantly.

--

--