babel-plugin-transform-optimize-object-literal

Rewrites object and array literals with JSON.parse calls.

Usage no npm install needed!

<script type="module">
  import babelPluginTransformOptimizeObjectLiteral from 'https://cdn.skypack.dev/babel-plugin-transform-optimize-object-literal';
</script>

README

babel-plugin-transform-optimize-object-literal

Node.js CI npm version

Because I heard JSON.parse is blazing fast https://v8.dev/blog/cost-of-javascript-2019#json

Input:

const foo = { a: "3", d: "f" };
const bar = [1, 4, -2, true, false, {}, "ok"];

Output:

const foo = JSON.parse('{"a":"3","d":"f"}');
const bar = JSON.parse('[1,4,-2,true,false,{},"ok"]');

Usage

yarn add -D babel-plugin-transform-optimize-object-literal

.babelrc:

{
  "plugins": [
    [
      "babel-plugin-transform-optimize-object-literal",
      {
        "skipArrayOptimizationIfLengthIsBelow": 0,
        "skipObjectOptimizationIfLengthIsBelow": 0
      }
    ]
  ]
}