Understanding Tree Shaking in JavaScript: A Quick Guide

August 20, 2024 (3mo ago)

As modern web applications grow in complexity, so does the need to optimize their performance. One powerful technique that has gained popularity in recent years is tree shaking. If you’ve heard the term but aren’t quite sure what it means or how it works, this post is for you.

What is Tree Shaking?

Tree shaking is a form of dead code elimination. It’s a technique used by JavaScript bundlers (like Webpack or Rollup) to remove code that is not actually being used (i.e., dead code) from your final bundle. Think about shaking a tree and dead leaves falling from it. This is precisely what is happening, except we're shaking out unused pieces of code to make our application lean and fast!

How Does Tree Shaking Work?

Tree shaking relies on ES6 modules, which use import and export statements. Unlike CommonJS, which loads entire modules even if you only use a small part, ES6 modules allow for static analysis of the code. This means that bundlers can determine which parts of your code are actually being used and which are not. Check this out to learn more about the difference between the two.

Here's an example:

// math.js
export function add(a, b) {
  return a + b;
}

export function multiply(a, b) {
  return a * b;
}

// main.js
import { add } from './math.js';

console.log(add(2, 3)); // Output: 5

In this case, if tree shaking is enabled, the multiply function from math.js will be removed from the final bundle because it is never used. This leads to faster load times and better performance by reducing the bundle size.

This is a small example, but on a big project when importing multiple large JavaScript bundles, load times become something you have to think about since bad load times lead to a negative user experience, especially when dealing with users on a slower network or device. Not everyone has the latest MacBook Pro!

How to Enable Tree Shaking

Luckily for us, there are bundlers that support tree shaking right out of the box...looking at ya'll Webpack and Rollup (wink, wink). Just make sure your code is written in ES6 modules.

Limitations of Tree Shaking

Now we've talked about how amazing this is, but there are a few limitations, namely:

  • Side Effects: If a module or its imports have side effects (e.g., modifying a global variable), tree shaking may not remove it, even if the code isn't directly used. You can counteract this by marking modules as "side-effect-free" in your package.json.
  • Non-ES6 Modules: Tree shaking only works with ES6 modules so if your project uses CommonJS or some other module system, it won't work.

Conclusion

Tree shaking is an essential optimization technique that helps reduce the size of your JavaScript bundles by eliminating unused code. Make sure you're using ES6 modules and using a bundle like Webpack or Rollup and you'll automatically benefit from tree shaking, leading to faster load times and a better user experience.

If you enjoyed this post, consider subscribing to my newsletter for more tips on JavaScript, web development, and more!