Live data from Hacker News

The cost of parsing JSON

v8.dev

11–20 of 308 posts

Re: The cost of parsing JSON

#11

Title correction: it's faster to parse and initialize using JSON than to parse and compile the code required to initialize Objects directly. If the code is already compiled, it's much faster to initialize in code. At least that's my understanding of the linked article.

That makes sense. The js object syntax is much more complex than json.

Re: The cost of parsing JSON

#12
Commenting the security issue from the end of explainer for visibility.

I’m having flashbacks to the Java serialize vulnerabilities from a couple years ago.

ECMAScript and JSON do not have the same set of escape characters:

``` Note: It’s crucially important to post-process user-controlled input to escape any special character sequences, depending on the context. In this particular case, we’re injecting into a tag, so we must (also) escape </script, <script, and <!- -. ```

Re: The cost of parsing JSON

#14

Title correction: it's faster to parse and initialize using JSON than to parse and compile the code required to initialize Objects directly. If the code is already compiled, it's much faster to initialize in code. At least that's my understanding of the linked article.

I ran it in JS perf and the myth is busted:

https://jsperf.com/jpvol

Re: The cost of parsing JSON

#15

Title correction: it's faster to parse and initialize using JSON than to parse and compile the code required to initialize Objects directly. If the code is already compiled, it's much faster to initialize in code. At least that's my understanding of the linked article.

I think your understanding is correct. The relevant quote: > 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.

So is this insight limited to literals at page load, or are there situations where it might be faster to construct a JSON string and then parse it compared to directly creating the object?

I'm thinking of compression libraries, or things like FlatBuffers.

Re: The cost of parsing JSON

#16

Title correction: it's faster to parse and initialize using JSON than to parse and compile the code required to initialize Objects directly. If the code is already compiled, it's much faster to initialize in code. At least that's my understanding of the linked article.

You're right, I didn't realize that.

Re: The cost of parsing JSON

#18
This is a terrible idea. 2x best-case performance is nothing. Especially not worth the extra hassle and complexity that it adds to the code. The sad thing about these kinds of articles is that there are people out there who will read this and actually start implementing it in their code.

Re: The cost of parsing JSON

#19
This makes total sense, it really just means that the time it takes to compile JSON.parse on a string literal is offset by how much simpler and faster parsing a JSON object is than a js one.
Post reply on HN