dvas0004 / NerdNotes

A collection of notes: things I'd like to remember while reading technical articles, technical questions I couldn't answer, and so on.
12 stars 0 forks source link

Performance: Parse JSON where possible #56

Open dvas0004 opened 5 years ago

dvas0004 commented 5 years ago

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.

dvas0004 commented 5 years ago

https://v8.dev/blog/cost-of-javascript-2019