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.
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.
Size Limit is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.
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.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.webpack plugin creates an empty webpack project, adds your library
and looks for the bundle size difference.time plugin compares the current machine performance with that of
a low-priced Android devices to calculate the CPU throttling rate.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.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 instructionsInstall the preset:
npm install --save-dev size-limit @size-limit/fileAdd 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 brotliedNow, 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.
Show instructionsInstall the preset:
npm install --save-dev size-limit @size-limit/preset-appAdd 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 msNow, 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.
Show instructionsInstall preset:
npm install --save-dev size-limit @size-limit/preset-big-libAdd 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 msNow, 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.
Show instructionsFirst, install size-limit:
npm install --save-dev size-limit @size-limit/preset-small-libAdd 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 brotliedIf your project size starts to look bloated, run --why for analysis:
npm run size -- --whyWe 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
save and compare previous size result
CLI does not work with workspaces and yarn pnp or pnpm without hoisting
Using `--why --save-bundle <DIR>` with multiple sections is buggy
Code coverage as a bundle metric
An NX plugin support
Fails if a file at the path provided in config does not exist. No way to opt out of this
Error checking package import bundle size for node packages with size-limit-preset-node-lib
Add `--save-bundle <dir>` to help and to README
Timed out after 30000 ms while trying to connect to the browser
support NodeJS ESM Module only project