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 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.
With GitHub action Size Limit will post bundle size changes as a comment
in pull request discussion.
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.
file, webpack, time)app, big-lib, small-lib).package.json and loads the config.webpack plugin, Size Limit will bundle your JS files intowebpack plugin creates an empty webpack project, adds your librarytime plugin compares the current machine performance with that oftime plugin runs headless Chrome (or desktop Chrome if it’sSuitable 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.
Install the preset:
npm install --save-dev size-limit @size-limit/file
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": "vitest && eslint ."
}
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 brotlied
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"
}
],
Add the size script to your test suite:
"scripts": {
"build": "webpack ./webpack.config.js",
"size": "npm run build && size-limit",
- "test": "vitest && eslint ."
+ "test": "vitest && eslint . && npm run size"
}
If you don’t have a continuous integration service running, don’t forget
to add one — start with GitHub Actions.
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.
Install the preset:
npm install --save-dev size-limit @size-limit/preset-app
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": "vitest && eslint ."
}
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 brotlied
Loading time: 602 ms on slow 3G
Running time: 214 ms on Snapdragon 410
Total time: 815 ms
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"
}
],
Add the size script to your test suite:
"scripts": {
"build": "webpack ./webpack.config.js",
"size": "npm run build && size-limit",
- "test": "vitest && eslint ."
+ "test": "vitest && eslint . && npm run size"
}
If you don’t have a continuous integration service running, don’t forget
to add one — start with GitHub Actions.
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.
Install preset:
npm install --save-dev size-limit @size-limit/preset-big-lib
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": "vitest && eslint ."
}
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 }"
}
],
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 brotlied
Loading time: 602 ms on slow 3G
Running time: 214 ms on Snapdragon 410
Total time: 815 ms
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"
}
],
Add a size script to your test suite:
"scripts": {
"build": "rollup ./scripts/rollup/build.js",
"size": "npm run build && size-limit",
- "test": "vitest && eslint ."
+ "test": "vitest && eslint . && npm run size"
}
If you don’t have a continuous integration service running, don’t forget
to add one — start with GitHub Actions.
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 brotlied).
+ [Size Limit](https://github.com/ai/size-limit) controls the size.
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.
First, install size-limit:
npm install --save-dev size-limit @size-limit/preset-small-lib
Add the size-limit section and the size script to your package.json:
+ "size-limit": [
+ {
+ "path": "index.js"
+ }
+ ],
"scripts": {
+ "size": "size-limit",
"test": "vitest && eslint ."
}
Here’s how you can get the size for your current project:
$ npm run size
Package size: 177 B with all dependencies, minified and brotlied
If your project size starts to look bloated, run --why for analysis:
npm run size -- --why
We use Statoscope as bundle analyzer.
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"
}
],
Add the size script to your test suite:
"scripts": {
"size": "size-limit",
- "test": "vitest && eslint ."
+ "test": "vitest && eslint . && npm run size"
}
If you don’t have a continuous integration service running, don’t forget
to add one — start with GitHub Actions.
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 brotlied). No dependencies.
+ [Size Limit](https://github.com/ai/size-limit) controls the size.
Size Limit has a GitHub action that comments and rejects pull requests based
on Size Limit output.
.github/workflows/size-limit.ymlname: '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 }}
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 Brotli (default), Gzip@size-limit/webpack adds your library to empty webpack projectfile plugin.@size-limit/webpack-why adds reports for webpack plugin@size-limit/webpack-css adds css support for webpack plugin.@size-limit/esbuild is like webpack plugin, but uses esbuildnode_modules.@size-limit/esbuild-why add reports for esbuild plugin@size-limit/time uses headless Chrome to track time to execute JS.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 and presets named starting with size-limit- are also supported.
For example:
size-limit-node-esbuild@size-limit/esbuild but for Node libraries.size-limit-preset-node-lib@size-limit/preset-small-lib but for Node libraries which containsnode-esbuild and core file plugins.nx-size-limitSize Limits supports three ways to define limits config.
size-limit section in package.json:
"size-limit": [
{
"path": "index.js",
"import": "{ createStore }",
"limit": "500 ms"
}
]
or a separate .size-limit.json config file:
;[
{
path: 'index.js',
import: '{ createStore }',
limit: '500 ms'
}
]
or a more flexible .size-limit.js or .size-limit.cjs config file:
module.exports = [
{
path: 'index.js',
import: '{ createStore }',
limit: '500 ms'
}
]
or types .size-limit.ts:
import type { SizeLimitConfig } from '../../packages/size-limit'
module.exports = [
{
path: 'index.js',
import: '{ createStore }',
limit: '500 ms'
}
] satisfies SizeLimitConfig
Note: TypeScript config files (.size-limit.ts) require
the jiti package to be installed:
npm install --save-dev jiti
Each section in the config can have these options:
"index.js", a pattern "dist/app-*.js"["index.js", "dist/app-*.js", "!dist/app-exclude.js"]."{ lib }"import { lib } from 'lib', * to test all exports,{ "a.js": "{ a }", "b.js": "{ b }" } to test multiple files.path option. It should be100 B, 10 kB, 500 ms, 1 s.false it will disable webpack.false it will disable calculating running time.true it will use Gzip compression and disablefalse it will disable any compression.stats.json from another build to compare--why is using)."@size-limit/webpack", "@size-limit/esbuild","@size-limit/time".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.
Also, you avoid having a config and pass the limit to CLI:
npm install --save-dev @size-limit/file
npx size-limit --limit "10 kB" dist/bundle.js
Additionally, you can specify a custom path to your configuration file when running the CLI:
npx size-limit --config configs/size-limit.json
--whyYou 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 & open it in the browser.
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.
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 }
})