A rollup plugin that wraps compatible objects with JSON.parse()
.
Anything that is not compatible or would be less than 1024 characters, is left unchanged.
It improves performance!
Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript. 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).
See this video on the chrome dev channel and the article on the V8 blog for more info.
npm install --save-dev rollup-plugin-json-parse
import { rollup } from 'rollup';
const rollupPluginJsonParse = require('rollup-plugin-json-parse');
export default {
input: 'main.js',
plugins: [
rollupPluginJsonParse({
minJSONStringSize: 1024 // default
})
]
});
The following assumes minJSONStringSize
of 0
for demonstration purposes.
Input
const a = {
prop1: () => {}, // can't be optimized
prop2: {
prop3: 2,
prop4: 'something',
['prop 5']: null
}
};
Output
const a = {
prop1: () => {}, // can't be optimized
prop2: /*@__PURE__*/JSON.parse("{\"prop3\":2,\"prop4\":\"something\",\"prop 5\":null}")
};