The story about a few imports
Tags: javascript, performance, webpack, webperf
Published: 2020-05-20 17:33:47

We all care about the performance of web applications that we build. We try to keep production bundles small and loading times low. That's a good thing! It definitely is for the user, who gets a great experience. But is it good for the developer? When we want the app to work fast, does it mean that creating it has to be slow? Can we still use external libraries and packages from NPM? Or do we have to write everything from scratch, counting each line of code?

Making a webpage fast may seem like a sacrifice from the developer's point of view. How could you keep a JavaScript bundle [below 50 kB](https://v8.dev/blog/cost-of-javascript-2019) when almost any popular library or framework takes half of that budget or even exceeds it? There is a way to find a compromise and keep both performance and easiness given by the ecosystem. Everything we need is the right set of tools.

## The story

Let's say we're writing dev.to news listing. For each article fetched from API we're supposed to display a title, beginning of the content, and a list of tags. Tags are returned from the API as a string, using a comma as a separator, so there are some transformations needed to parse and render them.

{% codesandbox webpack-stats-explorer-playground-article-a-z3pnl module=/src/article.jsx view=split %} 

This code is fairly simple, but it may be even more readable when written using predefined functions, like those form [`lodash`](https://www.npmjs.com/package/lodash) library. For many developers `lodash` is the very first choice when it comes to finding a comprehensive set of useful functions speeding-up the development.

```javascript
import _ from "lodash/fp";

const renderTags = _.pipe(
  _.split(","),
  _.map(_.trim),
  _.reject(_.isEmpty),
  _.map(tag => <li className={styles.tag}>{tag}</li>)
);
```

That looks quite neat! But there is a problem - bundle size increased from 12.5 kB to almost 94 kB 😱 Even if code quality could be considered as significantly better, such change would be unacceptable because it simply harms the user.

When we dig into the production bundle in [Webpack Stats Explorer](https://erykpiast.github.io/webpack-stats-explorer/), we can see a few modules were added, but there is one that should attract our attention - `lodash.min.js`. It takes almost 70 kB, the majority of our bundle! 

[![Webpack Stats Explorer showing added lodash library in the final bundle](https://dev-to-uploads.s3.amazonaws.com/i/x0y8bcuw45usyof4anox.png  "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Clodash%2Clodash.min.js&i=0&t=1)
*Click on the screenshot to launch [an interactive version of Webpack Stats Explorer](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Clodash%2Clodash.min.js&i=0&t=1)*

It turns out that by default, no matter how many functions we actually use from `lodash`, the whole library is sent down to the user. How to fix it? Named imports are the answer. Instead of importing the whole `_` object, we could specify each function we use by name. In the process called "tree shaking", Webpack will extract only the code that we need.

[![Webpack Stats Explorer showing default import changed to named](https://dev-to-uploads.s3.amazonaws.com/i/sxm2vv45pghpgxvel5sg.png "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Csrc%2Carticle.jsx&i=1&t=1)

There is some issue with this solution, though. `lodash` isn't really tree-shaking-friendly package, so by default switching to named imports changes nothing. To make it work as expected, we have to import each function from separate file. 

```javascript
import { pipe, map } from 'lodash/fp';
```

becomes  

```javascript
import pipe from 'lodash/fp/pipe';
import map from 'lodash/fp/map';
```

But this is a huge sacrifice, isn't it? The code doesn't look concise anymore and we start relying on the internal structure of `lodash` package instead of public API. Fortunately, instead of altering the code manually, it's enough to add a dedicated Babel plugin - [`babel-plugin-lodash`](https://www.npmjs.com/package/babel-plugin-lodash) and everything just works. We can keep using the named imports syntax.

```json
{
  "presets": [
    "@babel/preset-env",
    "babel-preset-preact"
  ],
  "plugins": ["babel-plugin-lodash"]
}
```

The plugin does the trick - bundle size goes down by 34 kB. [Webpack Stats Explorer shows](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Clodash&i=2&t=1), that instead of one big file, the bundle contains a lot of small modules. And those are the only ones we actually need.

[![Webpack Stats Explorer showing the bundle shrinking after adding babel-plugin-lodash](https://dev-to-uploads.s3.amazonaws.com/i/lb5n1dd6yjrk6kxun3hl.png "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Clodash&i=2&t=1)

So the bundle is now 57 kB. Is that good enough? Comparing to 12,5 kB we had before - not necessarily. There is another tool that may help - [`lodash-webpack-plugin`](https://www.npmjs.com/package/lodash-webpack-plugin).

```javascript
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const WebpackEnhancedStatsPlugin = require('webpack-enhanced-stats-plugin');

module.exports = {
  plugins: [
    new LodashModuleReplacementPlugin({ currying: true }),
    new WebpackEnhancedStatsPlugin({
      filename: 'stats.json'
    }),
```

Without any changes to the application code, it shrinks the bundle by another 23 kB. What kind of sorcery is this?! The whole trick is based on [substituting some of the internal library functions](https://github.com/lodash/lodash-webpack-plugin/blob/master/src/mapping.js) with simpler alternatives or even no-ops. There are plenty of options available but as our code is fairly simple, we need nothing more than currying. 

[![Webpack Stats Explorer showing how lodash-webpack-plugin wipes some internal modules out](https://dev-to-uploads.s3.amazonaws.com/i/kk474x1myf7v865hbelf.png "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Clodash&i=3&t=1)

After all those exertions, we managed to shrink the bundle to 34 kB - that's not bad. But it was 12,5 kB before. Is tripling the bundle size justified by better code readability and extensibility? I doubt! Fortunately, we can do better than that. `lodash` isn't the only library containing utility functions available on NPM and definitely not the tiniest one. [`nanoutils`](https://nanoutils.github.io/) may be a very decent drop-in replacement. This library helped me a lot in my daily job and I can recommend it to all searching for a utility package that doesn't damage the user experience.

[![Webpack Stats Explorer showing lodash replaced with nanoutils](https://dev-to-uploads.s3.amazonaws.com/i/6754wer6swp34lf6x07z.png "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules&i=4&t=1)

When we simply remove `lodash` with all build-time plugins and use raw `nanoutils`, the package shrinks by 4 kB. That's already a success, but not so impressive! We can do more than that. Similarly to `lodash`, by default `nanoutils` isn't tree-shakeable so we can shrink the bundle even more with [a Babel plugin](https://www.npmjs.com/package/babel-plugin-transform-imports).

```json
{
  "presets": [
    "@babel/preset-env",
    "babel-preset-preact"
  ],
  "plugins": [
    ["babel-plugin-transform-imports", {
      "nanoutils": {
        "transform": "nanoutils/lib/${member}",
        "preventFullImport": true
      }
    }]
  ]
}
```

Finally, the bundle has a size no bigger than 13,26 kB. It's only 700 B increase when comparing to the very first version which doesn't use any library. That looks more like a cost we can afford to increase code quality and not feel guilty about breaking user experience.

[![Webpack Stats Explorer showing results of applying Babel plugin enabling tree-shaking for nanoutils library](https://dev-to-uploads.s3.amazonaws.com/i/30af3wucxc7dehy3854s.png "Click on the screenshot to launch an interactive version of Webpack Stats Explorer")](https://erykpiast.github.io/webpack-stats-explorer/?s=&u=.%2Fa%2Fa.json&u=.%2Fa%2Fb.json&u=.%2Fa%2Fc.json&u=.%2Fa%2Fd.json&u=.%2Fa%2Fe.json&u=.%2Fa%2Ff.json&u=.%2Fa%2Fg.json&p=main%2Cnode_modules%2Cnanoutils&i=5&t=1)

## Conclusions

What lesson does the story tell to us? It is possible to have both performant (at least in terms of bundle size) and elegant code. There are a couple of things I wish you to remember.

### Tree-shaking and named imports

Tree-shaking is one of the greatest ideas since the sliced bread, at least in the world of web bundlers. It's supported [by Webpack](https://webpack.js.org/guides/tree-shaking/), but also [Rollup](https://rollupjs.org/guide/en/#tree-shaking) and [Parcel](https://medium.com/@devongovett/parcel-v1-9-0-tree-shaking-2x-faster-watcher-and-more-87f2e1a70f79#4ed3). To take advantage of tree-shaking, you should use named imports in favor of default one. Unless API of the library requires otherwise (ex. because it uses `this` under the hood), always write

```javascript
import { foo } from 'lib';

foo();
```

instead of

```javascript
import obj from 'lib';

obj.foo();
```

Make this syntax your new default.

### Build and analytic tools

A vast amount of modern libraries published to NPM is tree-shaking friendly. Unfortunately, for many of them, it isn't enabled by default. Use tools like [Webpack Bundle Analyzer](https://www.npmjs.com/package/webpack-bundle-analyzer) and [Webpack Stats Explorer](https://erykpiast.github.io/webpack-stats-explorer) to dig deep inside your production bundle and get to know what's exactly in it. If you find modules or pieces of code you suspect you don't need, try to use plugins like [babel-plugin-transform-imports](https://www.npmjs.com/package/babel-plugin-transform-imports) to get rid of them.

### Drop-in library replacements

For many packages, it's easy to find significantly smaller counterparts with similar functionality and API surface. It's very often the case for utility libraries, but also view frameworks. Think of [Preact](https://preactjs.com/) created to substitute [React](https://reactjs.org/). To estimate the size of the package before adding it to your project, you can use [Bundlephobia](https://bundlephobia.com/). For some libraries, the bottom section provides a shortlist of alternatives, which is also super helpful!

That's it! I hope you enjoyed the article and will have an opportunity to apply the described ideas to real web applications. Feel free to reach me in the comments if you have any questions!

## Disclaimer

I am a creator of [Webpack Stats Explorer](https://erykpiast.github.io/webpack-stats-explorer) - a free-to-use, open-source tool for developers who care about the performance of their web applications. Recently I also made some minor contributions to [`nanoutils`](https://nanoutils.github.io/).