• Stars
    star
    6,487
  • Rank 6,112 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

Calculate the real cost to run your JS app or lib to keep good performance. Show error in pull request if the cost exceeds the limit.

Size Limit Cult Of Martians

Size Limit logo by Anton Lovchikov

Size Limit is a performance budget tool for JavaScript. It checks every commit on CI, calculates the real cost of your JS for end-users and throws an error if the cost exceeds the limit.

  • ES modules and tree-shaking support.
  • Add Size Limit to Travis CI, Circle CI, GitHub Actions or another CI system to know if a pull request adds a massive dependency.
  • Modular to fit different use cases: big JS applications that use their own bundler or small npm libraries with many files.
  • Can calculate the time it would take a browser to download and execute your JS. Time is a much more accurate and understandable metric compared to the size in bytes.
  • Calculations include all dependencies and polyfills used in your JS.

Size Limit CLI

With GitHub action Size Limit will post bundle size changes as a comment in pull request discussion.

Size Limit comment in pull request about bundle size changes

With --why, Size Limit can tell you why your library is of this size and show the real cost of all your internal dependencies. We are using Statoscope for this analysis.

Statoscope example

Sponsored by Evil Martians

Who Uses Size Limit

How It Works

  1. Size Limit contains a CLI tool, 3 plugins (file, webpack, time) and 3 plugin presets for popular use cases (app, big-lib, small-lib). A CLI tool finds plugins in package.json and loads the config.
  2. If you use the webpack plugin, Size Limit will bundle your JS files into a single file. It is important to track dependencies and webpack polyfills. It is also useful for small libraries with many small files and without a bundler.
  3. The webpack plugin creates an empty webpack project, adds your library and looks for the bundle size difference.
  4. The time plugin compares the current machine performance with that of a low-priced Android devices to calculate the CPU throttling rate.
  5. Then the time plugin runs headless Chrome (or desktop Chrome if it’s available) to track the time a browser takes to compile and execute your JS. Note that these measurements depend on available resources and might be unstable. See here for more details.

Usage

JS Applications

Suitable for applications that have their own bundler and send the JS bundle directly to a client (without publishing it to npm). Think of a user-facing app or website, like an email client, a CRM, a landing page or a blog with interactive elements, using React/Vue/Svelte lib or vanilla JS.

Show instructions
  1. Install the preset:

    npm install --save-dev size-limit @size-limit/file
  2. Add the size-limit section and the size script to your package.json:

    + "size-limit": [
    +   {
    +     "path": "dist/app-*.js"
    +   }
    + ],
      "scripts": {
        "build": "webpack ./webpack.config.js",
    +   "size": "npm run build && size-limit",
        "test": "jest && eslint ."
      }
  3. Here’s how you can get the size for your current project:

    $ npm run size
    
      Package size: 30.08 kB with all dependencies, minified and gzipped
  4. Now, let’s set the limit. Add 25% to the current total size and use that as the limit in your package.json:

      "size-limit": [
        {
    +     "limit": "35 kB",
          "path": "dist/app-*.js"
        }
      ],
  5. Add the size script to your test suite:

      "scripts": {
        "build": "webpack ./webpack.config.js",
        "size": "npm run build && size-limit",
    -   "test": "jest && eslint ."
    +   "test": "jest && eslint . && npm run size"
      }
  6. If you don’t have a continuous integration service running, don’t forget to add one — start with Travis CI.

JS Application and Time-based Limit

File size limit (in kB) is not the best way to describe your JS application cost for developers. Developers will compare the size of the JS bundle with the size of images. But browsers need much more time to parse 100 kB of JS than 100 kB of an image since JS compilers are very complex.

This is why Size Limit support time-based limit. It runs headless Chrome to track the time a browser takes to compile and execute your JS.

Show instructions
  1. Install the preset:

    npm install --save-dev size-limit @size-limit/preset-app
  2. Add the size-limit section and the size script to your package.json:

    + "size-limit": [
    +   {
    +     "path": "dist/app-*.js"
    +   }
    + ],
      "scripts": {
        "build": "webpack ./webpack.config.js",
    +   "size": "npm run build && size-limit",
        "test": "jest && eslint ."
      }
  3. Here’s how you can get the size for your current project:

    $ npm run size
    
      Package size: 30.08 kB with all dependencies, minified and gzipped
      Loading time: 602 ms   on slow 3G
      Running time: 214 ms   on Snapdragon 410
      Total time:   815 ms
  4. Now, let’s set the limit. Add 25% to the current total time and use that as the limit in your package.json:

      "size-limit": [
        {
    +     "limit": "1 s",
          "path": "dist/app-*.js"
        }
      ],
  5. Add the size script to your test suite:

      "scripts": {
        "build": "webpack ./webpack.config.js",
        "size": "npm run build && size-limit",
    -   "test": "jest && eslint ."
    +   "test": "jest && eslint . && npm run size"
      }
  6. If you don’t have a continuous integration service running, don’t forget to add one — start with Travis CI.

Big Libraries

JS libraries > 10 kB in size.

This preset includes headless Chrome, and will measure your lib’s execution time. You likely don’t need this overhead for a small 2 kB lib, but for larger ones the execution time is a more accurate and understandable metric that the size in bytes. Libraries like React are good examples for this preset.

Show instructions
  1. Install preset:

    npm install --save-dev size-limit @size-limit/preset-big-lib
  2. Add the size-limit section and the size script to your package.json:

    + "size-limit": [
    +   {
    +     "path": "dist/react.production-*.js"
    +   }
    + ],
      "scripts": {
        "build": "webpack ./scripts/rollup/build.js",
    +   "size": "npm run build && size-limit",
        "test": "jest && eslint ."
      }
  3. If you use ES modules you can test the size after tree-shaking with import option:

      "size-limit": [
        {
          "path": "dist/react.production-*.js",
    +     "import": "{ createComponent }"
        }
      ],
  4. Here’s how you can get the size for your current project:

    $ npm run size
    
      Package size: 30.08 kB with all dependencies, minified and gzipped
      Loading time: 602 ms   on slow 3G
      Running time: 214 ms   on Snapdragon 410
      Total time:   815 ms
  5. Now, let’s set the limit. Add 25% to the current total time and use that as the limit in your package.json:

      "size-limit": [
        {
    +     "limit": "1 s",
          "path": "dist/react.production-*.js"
        }
      ],
  6. Add a size script to your test suite:

      "scripts": {
        "build": "rollup ./scripts/rollup/build.js",
        "size": "npm run build && size-limit",
    -   "test": "jest && eslint ."
    +   "test": "jest && eslint . && npm run size"
      }
  7. If you don’t have a continuous integration service running, don’t forget to add one — start with Travis CI.

  8. Add the library size to docs, it will help users to choose your project:

      # Project Name
    
      Short project description
    
      * **Fast.** 10% faster than competitor.
    + * **Small.** 15 kB (minified and gzipped).
    +   [Size Limit](https://github.com/ai/size-limit) controls the size.

Small Libraries

JS libraries < 10 kB in size.

This preset will only measure the size, without the execution time, so it’s suitable for small libraries. If your library is larger, you likely want the Big Libraries preset above. Nano ID or Storeon are good examples for this preset.

Show instructions
  1. First, install size-limit:

    npm install --save-dev size-limit @size-limit/preset-small-lib
  2. Add the size-limit section and the size script to your package.json:

    + "size-limit": [
    +   {
    +     "path": "index.js"
    +   }
    + ],
      "scripts": {
    +   "size": "size-limit",
        "test": "jest && eslint ."
      }
  3. Here’s how you can get the size for your current project:

    $ npm run size
    
      Package size: 177 B with all dependencies, minified and gzipped
  4. If your project size starts to look bloated, run --why for analysis:

    npm run size -- --why

    We use Statoscope as bundle analyzer.

  5. Now, let’s set the limit. Determine the current size of your library, add just a little bit (a kilobyte, maybe) and use that as the limit in your package.json:

     "size-limit": [
        {
    +     "limit": "9 kB",
          "path": "index.js"
        }
     ],
  6. Add the size script to your test suite:

      "scripts": {
        "size": "size-limit",
    -   "test": "jest && eslint ."
    +   "test": "jest && eslint . && npm run size"
      }
  7. If you don’t have a continuous integration service running, don’t forget to add one — start with Travis CI.

  8. Add the library size to docs, it will help users to choose your project:

      # Project Name
    
      Short project description
    
      * **Fast.** 10% faster than competitor.
    + * **Small.** 500 bytes (minified and gzipped). No dependencies.
    +   [Size Limit](https://github.com/ai/size-limit) controls the size.

Reports

Size Limit has a GitHub action that comments and rejects pull requests based on Size Limit output.

  1. Install and configure Size Limit as shown above.
  2. Add the following action inside .github/workflows/size-limit.yml
name: "size"
on:
  pull_request:
    branches:
      - master
jobs:
  size:
    runs-on: ubuntu-latest
    env:
      CI_JOB_NUMBER: 1
    steps:
      - uses: actions/checkout@v1
      - uses: andresz1/size-limit-action@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

Config

Plugins and Presets

Plugins or plugin presets will be loaded automatically from package.json. For example, if you want to use @size-limit/webpack, you can just use npm install --save-dev @size-limit/webpack, or you can use our preset @size-limit/preset-big-lib.

Plugins:

  • @size-limit/file checks the size of files with Gzip, Brotli or without compression.
  • @size-limit/webpack adds your library to empty webpack project and prepares bundle file for file plugin.
  • @size-limit/webpack-why adds reports for webpack plugin about your library is of this size to show the cost of all your dependencies.
  • @size-limit/webpack-css adds css support for webpack plugin.
  • @size-limit/esbuild is like webpack plugin, but uses esbuild to be faster and use less space in node_modules.
  • @size-limit/esbuild-why add reports for esbuild plugin about your library is of this size to show the cost of all your dependencies.
  • @size-limit/time uses headless Chrome to track time to execute JS.
  • @size-limit/dual-publish compiles files to ES modules with dual-publish to check size after tree-shaking.

Plugin presets:

  • @size-limit/preset-app contains file and time plugins.
  • @size-limit/preset-big-lib contains webpack, file, and time plugins.
  • @size-limit/preset-small-lib contains esbuild and file plugins.

Third-Party Plugins

Third-party plugins and presets named starting with size-limit- are also supported. For example:

Limits Config

Size Limits supports three ways to define limits config.

  1. size-limit section in package.json:

      "size-limit": [
        {
          "path": "index.js",
          "import": "{ createStore }",
          "limit": "500 ms"
        }
      ]
  2. or a separate .size-limit.json config file:

    [
      {
        "path": "index.js",
        "import": "{ createStore }",
        "limit": "500 ms"
      }
    ]
  3. or a more flexible .size-limit.js or .size-limit.cjs config file:

    module.exports = [
      {
        path: "index.js",
        import: "{ createStore }",
        limit: "500 ms"
      }
    ]

Each section in the config can have these options:

  • path: relative paths to files. The only mandatory option. It could be a path "index.js", a pattern "dist/app-*.js" or an array ["index.js", "dist/app-*.js", "!dist/app-exclude.js"].
  • import: partial import to test tree-shaking. It could be "{ lib }" to test import { lib } from 'lib', * to test all exports, or { "a.js": "{ a }", "b.js": "{ b }" } to test multiple files.
  • limit: size or time limit for files from the path option. It should be a string with a number and unit, separated by a space. Format: 100 B, 10 kB, 500 ms, 1 s.
  • name: the name of the current section. It will only be useful if you have multiple sections.
  • entry: when using a custom webpack config, a webpack entry could be given. It could be a string or an array of strings. By default, the total size of all entry points will be checked.
  • webpack: with false it will disable webpack.
  • running: with false it will disable calculating running time.
  • gzip: with false it will disable gzip compression.
  • brotli: with true it will use brotli compression and disable gzip compression.
  • config: a path to a custom webpack config.
  • ignore: an array of files and dependencies to exclude from the project size calculation.
  • modifyWebpackConfig: (.size-limit.js only) function that can be used to do last-minute changes to the webpack config, like adding a plugin.
  • compareWith: path to stats.json from another build to compare (when --why is using).
  • uiReports: custom UI reports list (see Statoscope docs).

If you use Size Limit to track the size of CSS files, make sure to set webpack: false. Otherwise, you will get wrong numbers, because webpack inserts style-loader runtime (≈2 kB) into the bundle.

Analyze with --why

You can run size-limit --why to analyze the bundle.

You will need to install @size-limit/esbuild-why or @size-limit/webpack-why depends on which bundler you are using (default is esbuild).

For @size-limit/esbuild-why, it will generate a esbuild-why.html at the current directory.

If you also specify --save-bundle <DIR>, the report will be generated inside <DIR>.

If you have multiple sections in your config, the files will be named esbuild-why-{n}.html, or you can give it a custom name:

[
  {
    "name": "cjs",
    /* snap */
  },
  {
    "name": "esm",
    /* snap */
  }
]

This will produce esbuild-why-cjs.html and esbuild-why-esm.html respectively.

For @size-limit/webpack-why, it will generate the report and open it in the browser automatically.

JS API

const sizeLimit = require('size-limit')
const filePlugin = require('@size-limit/file')
const webpackPlugin = require('@size-limit/webpack')

sizeLimit([filePlugin, webpackPlugin], [filePath]).then(result => {
  result //=> { size: 12480 }
})

More Repositories

1

nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
JavaScript
24,064
star
2

easings.net

Easing Functions Cheat Sheet
CSS
7,908
star
3

visibilityjs

Wrapper for the Page Visibility API
JavaScript
1,825
star
4

nanoevents

Simple and tiny (107 bytes) event emitter library for JavaScript
TypeScript
1,442
star
5

autoprefixer-rails

Autoprefixer for Ruby and Ruby on Rails
Ruby
1,213
star
6

nanocolors

Use picocolors instead. It is 3 times smaller and 50% faster.
JavaScript
868
star
7

audio-recorder-polyfill

MediaRecorder polyfill to record audio in Edge and Safari
JavaScript
580
star
8

keyux

JS library to improve keyboard UI of web apps
TypeScript
380
star
9

webp-in-css

PostCSS plugin and tiny JS script (131 bytes) to use WebP in CSS background
JavaScript
346
star
10

offscreen-canvas

Polyfill for OffscreenCanvas to move Three.js/WebGL/2D canvas to Web Worker
JavaScript
332
star
11

convert-layout

JS library to convert text from one keyboard layout to other
JavaScript
251
star
12

ssdeploy

Netlify replacement to deploy simple websites with better flexibility, speed and without vendor lock-in
JavaScript
194
star
13

environment

My home config, scripts and installation process
Shell
193
star
14

nanodelay

A tiny (37 bytes) Promise wrapper around setTimeout
JavaScript
189
star
15

dual-publish

Publish JS project as dual ES modules and CommonJS package to npm
JavaScript
186
star
16

nanospy

Spy and mock methods in tests with great TypeScript support
TypeScript
138
star
17

check-dts

Unit tests for TypeScript definitions in your JS open source library
JavaScript
138
star
18

autoprefixer-core

autoprefixer-core was depreacted, use autoprefixer
JavaScript
136
star
19

transition-events

jQuery plugin to set listeners to CSS Transition animation end or specific part
JavaScript
133
star
20

evil-blocks

Tiny framework for web pages to split your app to separated blocks
JavaScript
127
star
21

rails-sass-images

Sass functions and mixins to inline images and get images size
Ruby
114
star
22

compass.js

Compass.js allow you to get compass heading in JavaScript by PhoneGap, iOS API or GPS hack.
CoffeeScript
112
star
23

evil-front

Helpers for frontend from Evil Martians
Ruby
101
star
24

rake-completion

Bash completion support for Rake
Shell
63
star
25

yaspeller-ci

Fast spelling check for Travis CI
JavaScript
61
star
26

jquery-cdn

Best way to use latest jQuery in Ruby app
Ruby
59
star
27

sitnik.ru

My homepage content and scripts
JavaScript
57
star
28

pages.js

CoffeeScript
44
star
29

fotoramajs

Fotorama for Ruby on Rails
Ruby
44
star
30

about-postcss

Keynotes about PostCSS
Ruby
29
star
31

autohide-battery

GNOME Shell extension to hide battery icon in top panel, if battery is fully charged and AC is connected.
JavaScript
28
star
32

darian

Darian Mars calendar converter
Ruby
25
star
33

better-node-test

The CLI shortcut for node --test runner with TypeScript
JavaScript
25
star
34

plain_record

Data persistence with human readable and editable storage.
Ruby
24
star
35

evolu-lang

Programming language to automatically generate programs by evolution (genetic programming).
JavaScript
22
star
36

martian-logux-demo

TypeScript
17
star
37

hide-keyboard-layout

GNOME Shell extension to hide keyboard layout indicator in status bar
JavaScript
17
star
38

twitter2vk

Script to automatically repost statuses from Twitter to VK (В Контакте)
Ruby
16
star
39

asdf-cache-action

A Github Action to install runtimes by asdf CLI with a cache
15
star
40

ci-job-number

Return CI job number to run huge tests only on first job
JavaScript
15
star
41

print-snapshots

Print Jest snapshots to check CLI output of your tool
JavaScript
15
star
42

load-resources

Load all JS/CSS files from site website
JavaScript
15
star
43

susedko

Fedora CoreOS ignition config for my home server
JavaScript
14
star
44

file-container

Store different languages in one source file
JavaScript
14
star
45

postcss-isolation

Fix global CSS with PostCSS
14
star
46

autoprefixer-cli

CLI for Autoprefixer
JavaScript
14
star
47

d2na

D²NA language for genetic programming
Ruby
11
star
48

showbox

Keynote generator
JavaScript
11
star
49

boilerplates

Boilerplate for my open source projects
JavaScript
9
star
50

postcss-way

Keynotes about PostCSS way
9
star
51

gulp-bench-summary

Display gulp-bench results in nice table view
JavaScript
8
star
52

universal-layout

Универсальная раскладка Ситника
8
star
53

anim2012

Доклад «Анимации по-новому — лень, гордыня и нетерпимость»
CSS
8
star
54

nanopurify

A tiny (from 337 bytes) HTML sanitizer
JavaScript
7
star
55

ai

6
star
56

rit3d

Доклад «Веб, теперь в 3D: Практика»
CSS
6
star
57

dis.spbstu.ru

Department homepage
Ruby
5
star
58

jstransformer-lowlight

Lowlight support for JSTransformers
JavaScript
5
star
59

jest-ci

CLI for Jest test framework, but coverage only on first CI job
JavaScript
5
star
60

evolu-steam

Evolu Steam – evolutionary computation for JavaScript
JavaScript
5
star
61

insomnis

Текст блогокниги «Инсомнис»
4
star
62

plague

Blog/book Plague engine
Ruby
4
star
63

wsd2013

Презентация «Автопрефиксер: мир без CSS-префиксов»
Ruby
4
star
64

showbox-bright

Shower Bright theme for Showbox
JavaScript
3
star
65

ruby2jar

Ruby2Jar builds JAR from a Ruby script
Ruby
3
star
66

showbox-ai

Sitnik’s theme for ShowBox
CSS
3
star
67

showbox-shower

Shower for ShowBox
JavaScript
2
star
68

on_the_islands

Ruby
2
star