• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Measure the real, minified, tree-shaken impact of individual imports into your app

import-size

Measure the real, minified, tree-shaken impact of individual imports into your app

Buy Me A Coffee

Usage

npx import-size <dependency> <imports>. For example:

npx import-size immer produce enablePatches

npx import-size --report immer produce enableES5 enablePatches enableMapSet

examle report

If you want to generate reports as part of your tooling, you might want to import-size as development dependency using yarn or npm.

Why

This tool is basically the result of a long-winded twitter thread. A long story short: this tool will calculate how many bytes a dependency costs, after bundling it for production and apply-ing treeshaking, based on the things you actually want to import.

import-size fixes that and comes as a convenient CLI tool. It wraps the excellent import-cost package (also available as VS Code plugin). This tool will create a mini project, that imports precisely the requested imports using production tree-shaking settings.

This is not only useful to monitor your app's size budget, but also when you are maintaining a library. Especially in "report" mode, see below

Examples

$ npx import-size mobx autorun observable computed action
13337

Using only autorun, observable, computed and action from "MobX" will result in 13 KB (gzipped) being added to your bundle

$ npx import-size mobx '*'
15850

Importing the full MobX api will take 2KB more (so clearly it is not super good at tree-shaking (yet!))

You can also point the tool at any local directory that contains a module source. For example, running npx import-size . autorun in the MobX directory will measure the size of autorun in the directory in which you run this command. (Do make sure that the library has been build locally in the latter case)

To measure the size of a default export, just pass in default as method name.

To measure the size of all exports, pass in *. To prevent bash expanding this into a bunch of file names, you will typically have to quote it like: '*'.

The sizes displayed are gzipped and include transitive dependencies (regardless whether these deps are shared with other libs). Sizes are always a few (~20) bytes off due to utility code that is injected.

Report mode

Report mode can break down the costs of a library as you import more and more of it's features:

$ import-size --report immer produce enableES5 enablePatches enableMapSet

Import size report for immer:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        (index)         β”‚ just this β”‚ cumulative β”‚ increment β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ import * from <module> β”‚   6684    β”‚     0      β”‚     0     β”‚
β”‚        produce         β”‚   4024    β”‚    4024    β”‚     0     β”‚
β”‚       enableES5        β”‚   2428    β”‚    4895    β”‚    871    β”‚
β”‚     enablePatches      β”‚   1840    β”‚    5666    β”‚    771    β”‚
β”‚      enableMapSet      β”‚   4915    β”‚    6489    β”‚    823    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
(this report was generated by npmjs.com/package/import-size)

The report mode first reports the cost of doing a 'side effect import' and a '*' (everything) import.

The rows after that show the import sizes of all mentioned imports:

  1. individually in the first column
  2. combined with the above rows
  3. the delta of that

So the above report shows that just importing enablePatches from 'immer' will cost you 1840 bytes. But, it will cost only an additional 871 bytes if you are already importing produce and enableES5 anyway. In other words, if you just use produce from Immer it will arrive at 4KB, and the three additional opt-in features will all come at roughly 1 additional KB (note that those numbers are not accurate for Immer, they are just a demonstration).

Also note that the order of arguments does matter for the cumulatives and increment columns, so you typically want to put the imports in an order that make sense to future library users. Reports like these might be useful in documentation.

More Repositories

1

relative-deps

Installs local dependencies for optimal developer experience
JavaScript
428
star
2

use-st8

Single-function alternative for React.useState
TypeScript
229
star
3

redux-todomvc

Redux todoMVC, used to do some benchmarking
JavaScript
106
star
4

state-routing-blog-sources

JavaScript
88
star
5

remmi

Nothing to see here. Go away
TypeScript
86
star
6

rval

minimalistic transparent reactive programming library
TypeScript
85
star
7

react-mobx-shop

71
star
8

mst-course

Lesson sources for "Manage Application State with Mobx-state-tree" course
JavaScript
62
star
9

nscript

Write shell scripts like a bearded guru by unleashing your javascript skills!
JavaScript
60
star
10

mobx-todomvc

Trimmed down TodoMVC used for benchmarking
JavaScript
45
star
11

ts-npm-lint

Utility to help you to create typescript based npm modules. Can also strip reference paths.
JavaScript
38
star
12

willing-people

People you might want to ask instead of me
19
star
13

immer-gifts

Example accompanying the Immer course on egghead
JavaScript
19
star
14

react-next-2017-demo

JavaScript
10
star
15

miniup

Light-weight easy-to-use PEG parser with some toppings!
TypeScript
8
star
16

flipper-data-source-demo

TypeScript
6
star
17

NOA

A reflective data and calculating model for javascript
TypeScript
4
star
18

reactive2016-slides

The Quest For Immer Mutable State Management
HTML
4
star
19

math-tester

For my kids
TypeScript
4
star
20

mxgit

[unmaintained] Small tool that helps versioning Mendix projects with git.
JavaScript
4
star
21

react-ts-conversion-exercise

JavaScript
4
star
22

magix-mobx-slides

Slides from RuhrJS 2016 "Magic MobX" presentation
JavaScript
2
star
23

currencies-demo

JavaScript
2
star
24

clutility

The minimalistic class and inheritance utility for javascript.
JavaScript
2
star
25

flowmachine

TypeScript
1
star
26

reactjsday2017-presentation

HTML
1
star
27

react-next-2017-slides

HTML
1
star
28

reatjsday2017-demo

JavaScript
1
star