Sitemap
Harsha attray

Harsha attray

In a sincere effort to fill in the blanks - https://harshat.space/

Performance Optimization and Code Splitting

2 min readJul 15, 2018
Press enter or click to view image in full size

Performance of a website is heavily dependent on the amount of assets we ship with it , and more often than not it comes down to heavy bundles being pushed on first load, overburdened with code that might not even be needed for that initial page load.

Incase of relatively slower connections and especially on mobile devices this seemingly normal process tends to get magnified and what we get is a webpage gasping to see the first light and a user who has already given up.

If we want to see the exact amount of unused code in any given webpage, we can turn over to the Code Coverage feature of the chrome dev tools. It gives you a clear picture of what percentage of code that is being shipped in the current page load is actually being used for that purpose.

Press enter or click to view image in full size
Chrome Dev tools code coverage

Let us consider the above snapshot, the initial load size is about 5.2 MB and 43% of it is unused and not needed for that page to load. Imagine the performance upswing that can be achieved by reducing that size and relieving it of most of the unused code if not all of it.

Get Harsha attray’s stories in your inbox

Join Medium for free to get updates from this writer.

One solution that webpack offers to this problem is code-splitting. It essentially splits our big blob of JS into asynchronous chunks at build-time, so that these can be selectively called if and only when they are required.

Code-splitting can be implemented statically or dynamically, even though the dynamic code splitting essentially contains a static relative path.

Press enter or click to view image in full size
A simple overview of a code-splitting block

As seen in the above snippet, we import a specific JS file using the dynamic import syntax and trigger it later, in this case a click event.The dynamic import from any JS file takes in the default exports that are declared within it. getSpecificChunk returns a promise which when resolved gives us the contents of that imported file which is requested asynchronously in the form of a new chunk.

Employing Code-splitting to large codebases can drastically improve load times, impacting the overall performance.

--

--