|
| 1 | +# json-perf-loader |
| 2 | + |
| 3 | +[![npm][npm]][npm-url] |
| 4 | +[![node][node]][node-url] |
| 5 | +[![deps][deps]][deps-url] |
| 6 | +[![size][size]][size-url] |
| 7 | + |
| 8 | +A loader for webpack to load JSON with performance advice. |
| 9 | + |
| 10 | +## The cost of parsing JSON |
| 11 | + |
| 12 | +See [The cost of parsing JSON - V8](https://v8.dev/blog/cost-of-javascript-2019#json) |
| 13 | + |
| 14 | +> Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript. |
| 15 | +> This knowledge can be applied to improve start-up performance for web apps that ship large JSON-like configuration object literals (such as inline Redux stores). |
| 16 | +> Instead of inlining the data as a JavaScript object literal. |
| 17 | +> |
| 18 | +> As long as the JSON string is only evaluated once, the `JSON.parse` approach is much faster compared to the JavaScript object literal, especially for cold loads. |
| 19 | +> A good rule of thumb is to apply this technique for objects of **10 kB or larger** — but as always with performance advice, measure the actual impact before making any changes. |
| 20 | +
|
| 21 | +## Getting Started |
| 22 | + |
| 23 | +To begin, you'll need to install `json-perf-loader`: |
| 24 | + |
| 25 | +```shell |
| 26 | +$ npm install json-perf-loader --save-dev |
| 27 | +``` |
| 28 | + |
| 29 | +`json-perf-loader` works like |
| 30 | +[`json-loader`](https://github.com/justjavac/json-loader), but much faster. |
| 31 | + |
| 32 | +**index.js** |
| 33 | + |
| 34 | +```js |
| 35 | +import json from './file.json'; |
| 36 | +``` |
| 37 | + |
| 38 | +**webpack.config.js** |
| 39 | + |
| 40 | +```js |
| 41 | +module.exports = { |
| 42 | + module: { |
| 43 | + rules: [ |
| 44 | + { |
| 45 | + test: /\.json$/i, |
| 46 | + type: "javascript/auto", |
| 47 | + use: [ |
| 48 | + { |
| 49 | + loader: 'json-perf-loader', |
| 50 | + options: { |
| 51 | + limit: 4096, |
| 52 | + }, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +And run `webpack` via your preferred method. |
| 62 | + |
| 63 | +**Note: `type: "javascript/auto"` is require**. See https://webpack.js.org/configuration/module/#ruletype |
| 64 | + |
| 65 | +> `Rule.type` sets the type for a matching module. |
| 66 | +> This prevents defaultRules and their default importing behaviors from occurring. |
| 67 | +> For example, if you want to load a `.json` file through a custom loader, you'd need to set the `type` to `javascript/auto` to bypass webpack's built-in json importing. |
| 68 | +
|
| 69 | +## Options |
| 70 | + |
| 71 | +### `limit` |
| 72 | + |
| 73 | +Type: `Number|String` |
| 74 | +Default: `1024 * 10` |
| 75 | + |
| 76 | +The limit can be specified via loader options and defaults to `1024 * 10`. This is the recommended value for the V8 team. |
| 77 | + |
| 78 | +#### `Number` |
| 79 | + |
| 80 | +A `Number` specifying the maximum size of a file in bytes. If the file size is |
| 81 | +**equal** or **greater** than the limit `JSON.parse` will be used. |
| 82 | + |
| 83 | +**webpack.config.js** |
| 84 | + |
| 85 | +```js |
| 86 | +module.exports = { |
| 87 | + module: { |
| 88 | + rules: [ |
| 89 | + { |
| 90 | + test: /\.json$/i, |
| 91 | + type: "javascript/auto", |
| 92 | + use: [ |
| 93 | + { |
| 94 | + loader: 'json-perf-loader', |
| 95 | + options: { |
| 96 | + limit: 10, |
| 97 | + }, |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +[MIT](./LICENSE) |
| 109 | + |
| 110 | +[npm]: https://img.shields.io/npm/v/json-perf-loader.svg |
| 111 | +[npm-url]: https://npmjs.com/package/json-perf-loader |
| 112 | +[node]: https://img.shields.io/node/v/json-perf-loader.svg |
| 113 | +[node-url]: https://nodejs.org |
| 114 | +[deps]: https://david-dm.org/justjavac/json-perf-loader.svg |
| 115 | +[deps-url]: https://david-dm.org/justjavac/json-perf-loader |
| 116 | +[tests]: https://dev.azure.com/justjavac/json-perf-loader/_apis/build/status/justjavac.json-perf-loader?branchName=master |
| 117 | +[tests-url]: https://dev.azure.com/justjavac/json-perf-loader/_build/latest?definitionId=2&branchName=master |
| 118 | +[cover]: https://codecov.io/gh/justjavac/json-perf-loader/branch/master/graph/badge.svg |
| 119 | +[cover-url]: https://codecov.io/gh/justjavac/json-perf-loader |
| 120 | +[size]: https://packagephobia.now.sh/badge?p=json-perf-loader |
| 121 | +[size-url]: https://packagephobia.now.sh/result?p=json-perf-loader |
0 commit comments