Tree shaking is an optimization technique that automatically detects and removes unused JavaScript code during the build process of your website. The name refers to shaking a tree where dead branches fall off. In practice, a bundler like Webpack, Rollup or Vite analyzes which functions, variables and modules you actually use and omits anything you don't call on in the final file. For an average SMB Web site, this means that your JavaScript files can be 30 to 60 percent smaller, which directly affects load speed and Core Web Vitals score.
How tree shaking works in modern bundlers
Tree shaking relies on ES6 module syntax (import and export) because it is statically analyzable. While building your site, the bundler scans all import statements and marks which exports are actually called. Code that is not imported or called anywhere is labeled dead code and is not included in the production file. This process works reliably only if you use libraries that provide ES modules and if you don't use dynamic imports or eval constructs that interfere with static analysis. Modern frameworks such as React, Vue and Svelte support tree shaking natively, but older CommonJS modules often remain entirely in your bundle.
Why tree shaking is now standard in web development
Around 2015, Rollup introduced tree shaking as a core feature, followed by Webpack 2 in 2017. The answer to a growing problem: JavaScript bundles were getting bigger and bigger through the use of extensive libraries like Lodash or Moment.js, while websites often only needed a handful of functions. Google made it clear in 2018 that load speed is a ranking factor and introduced Core Web Vitals in 2020. Since then, tree shaking is no longer a nice-to-have but a basic requirement for any professional website. An unoptimized 800 kB bundle can shrink to 250 kB due to tree shaking, which makes the difference between an acceptable and a slow mobile experience.
What tree shaking brings to Dutch SME websites
In practice, we at Monkey Vision see that tree shaking mainly impacts websites with many interactive components or extensive dashboards. An ecommerce store that loads a datepicker, validation library and analytics script, without tree shaking, easily sends 600 kB of JavaScript to the visitor, while only 40 percent of it is actually executed. By enabling tree shaking in combination with code splitting, you achieve a First Contentful Paint that is 1 to 2 seconds faster, which has a measurable effect on bounce rates and conversion. For those serious about investing in speed and user experience, professional front-end development with optimized build processes is the logical next step. A link to API integrations is also relevant, as tree shaking helps to load only the necessary SDK components. More background on optimization can be found in Google Developers' JavaScript Optimization Guide.