Skip to main content
View on YouTube to comment and like!

Syntax Video

Tree Shaking and Code Splitting Explained | Syntax STUMP'D

Chapter 1 00:00

Transcript

what's up CJ here in this video I'm going to explain tree shaking and code splitting which are optimization techniques for frontend codes that came up in episode 802 of syntax so in this episode Scott and West were asking each other a bunch of interview questions and the first one I'm going to cover was the one about tree shaking my next question for you is explain the concept of tree shaking in modern JavaScript bundlers how does it work and what are the benefits so tree shaking isn't specific to JavaScript it is a type of dead code elimination which is a process used by transpilers and compilers that can essentially remove or get rid of any unused code and this is important when we're bundling and building front-end websites because we don't want to ship a bunch of JavaScript we to the front end we only want to ship JavaScript that is actually used in our application and in modern codebases you're probably using a build tool like vit or webpack and these tools do tree shaking by default so vit uses roll up under the hood for production builds and tree shaking is enabled by default so to see a real

world example let's actually pull in some functions from this date functions Library so this library has a bunch of functions for formatting and working with dates um and let's actually pull the code from the homepage here so in my code example I've imported those functions I'm executing them getting back the result and then showing the result on the page and if we take a look at the app with no tree shaking so tree shaking has been disabled we can see that the resulting bundle size here is 46 kiloby and it's so big because it included the entire date functions library but we don't want that right we're only importing these functions so we would want our resulting bundle to only include these functions and their dependencies now there's a really good stack Overflow answer that talks about tree shaking and it kind of shows a visual of what's Happening Here and the reason tree shaking can happen with ecmascript modules is because the imports and exports and all of our code can be statically analyzed that means the bundler can actually follow those imports and exports and basically build up a tree of all the dependencies so we

can see that our entry point Imports some other module like date functions date functions import some other modules like the actual functions that we're using Etc and so the resulting code instead of having the full tree of imports only has the ones that are actually used in our application now if we take a look at the same example but with tree shaking enabled we can see that the bundle size is only 25 kiloby so with tree shaking we've essentially eliminated half of that code because it's not being used at all now a great way to visualize this is with a plugin called visualizer that you can add to roll up and so if we take a look at the visualization for the non- Tre Shakeen version we can see that the entire date functions module here gets included and so it has every single function all their dependencies but if we take a look at the tree shaken version we can see that the date functions module only has the things that are required for the functions that I imported so it has the functions I imported but then there are some internal dependencies of those functions that it needs to include as well but in this example we cut our bundle size in half just by enabling

tree shaking now the next question had to do with code splitting which is another optimization technique explain the concept of code splitting in modern JavaScript applications how does it work what are the performance implications additionally describe how code splitting interacts with lazy loading so for this example I have a button on the page called load Pokemon and that is actually bringing in a separate module so you can see in my source codes I have another file is called load Pokemon and this just exports a function that has a bunch of data inside of it so it's not doing an API request or anything like that but this one file does have over 750 lines of code and we don't need it until the button on the page is pressed so with code splitting I can make it so that this module isn't loaded until it's actually needed and we do that with a dynamic import so you can see here when that button is clicked I'm actually importing that module grabbing the function and then actually invoking that function so if we take a look at this in the browser when I click this button you

can see that the browser actually made the request for that Javascript file and part of the build process actually created multiple bundles you had our initial bundle that gets loaded when the page loads then you had a separate bundle that can be loaded dynamically when it's needed now this is also very common in like single page applications where you might have multiple pages and you for instance don't need to load the data for the about page when the homepage is loaded and another example I have to show you here is with View and view router now this would still apply if you're using like react and react router but essentially we want to only load the JavaScript for the page when that page is loaded and you can see in this view app I have two pages I have my homepage and my about page and with view router you can set up your config here like this so basically I have my home route that's going to load that homepage but then the about page is going to be dynamically loaded so you can see I'm using that import keyword here but essentially the JavaScript code for the about page will not be loaded until I actually click in and go to the about page and so this is also important for

decreasing the amount of JavaScript that's loaded when the user visits your website because some of that JavaScript is only needed on certain pages and with Dynamic Imports we can get that so that's it for this stumped explainer if there were any other questions that were asked in 802 or any of the other stumped episodes that you'd like to see me dive deeper into let me know down in the comments all right I'll see you in the next one [Music]

Quote it with credit and a link. Quoting Syntax