Skip to content

A rollup plugin that wraps compatible objects with JSON.parse() to improve performance.

License

Notifications You must be signed in to change notification settings

tjenkinson/rollup-plugin-json-parse

Repository files navigation

npm version

rollup-plugin-json-parse

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.

Why?

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.

Chrome dev YouTube video

Installation

npm install --save-dev rollup-plugin-json-parse

Usage

import { rollup } from 'rollup';
const rollupPluginJsonParse = require('rollup-plugin-json-parse');

export default {
  input: 'main.js',
  plugins: [
    rollupPluginJsonParse({
      minJSONStringSize: 1024 // default
    })
  ]
});

Example

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}")
};

About

A rollup plugin that wraps compatible objects with JSON.parse() to improve performance.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published