Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript.
Instead of inlining the data as a JavaScript object literal, like so:
const data = { foo: 42, bar: 1337 }; // 🐌
…it can be represented in JSON-stringified form, and then JSON-parsed at runtime:
const data = JSON.parse('{"foo":42,"bar":1337}'); // 🚀
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.
Because the JSON grammar is much simpler than JavaScript’s grammar, JSON can be parsed more efficiently than JavaScript.
Instead of inlining the data as a JavaScript object literal, like so:
…it can be represented in JSON-stringified form, and then JSON-parsed at runtime:
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.