News for hackers. Stuff that matters.
Re:The Cost of JavaScript in 2019 by outside1234 on Sunday June 30, 2019 @02:12PM (#20319555)
I hate that this micro performance hack actually is better - why don’t they just fix this in the JIT so that the literal is just as fast as the JSON parse? Why do we have to think about this?
Re:The Cost of JavaScript in 2019 by bzbarsky on Sunday June 30, 2019 @05:51PM (#20320769)
This is happening way before the JIT phase, at the parsing phase.

And the reason it can't be as fast as a JSON parse is explained in the article: The grammar of JSON is much simpler. The literal parser in normal JS needs to deal with things like:

  const data = { foo: /* something*/ 42, [barRef]: 1337 };
so needs to do more branching than a JSON parser. And yes, you can pre-scan the string to make sure it's not doing anything like that... but that probably already costs more than the tokenizer phase of a JSON parse, and it's overhead you still need to do that a JSON parse doesn't have to do.
Re:The Cost of JavaScript in 2019 by badamp on Sunday June 30, 2019 @02:33PM (#20319659)
It isn’t a jit issue it is a parsing issue.

Also micro performance becomes macro at scale. This is not something that you worry or think about with a handful of literals. It would be good for an inner loop or code generator.

Re:The Cost of JavaScript in 2019 by whatshisface on Sunday June 30, 2019 @03:09PM (#20319868)
You wouldn't write this in an inner loop, you would do it once in the entire program. It's a literal object that doesn't change.
Re:The Cost of JavaScript in 2019 by baddox on Sunday June 30, 2019 @02:43PM (#20319718)
The runtime can't know whether it's parsing a plain old JSON-compatible object literal until it, well, parses it.
Re:The Cost of JavaScript in 2019 by epicureanideal on Sunday June 30, 2019 @04:21PM (#20320273)
Sounds like some sort of compiler hint like ‘expect json’ or ‘use json’ right above a json literal would be useful. Assuming it matters enough to be worth it.
Re:The Cost of JavaScript in 2019 by vbezhenar on Sunday June 30, 2019 @04:23PM (#20320282)
JSON.parse sounds like a good compiler hint :)
Re:The Cost of JavaScript in 2019 by pbhjpbhj on Sunday June 30, 2019 @02:52PM (#20319765)
IANAProgrammer but couldn't it do like try..catch and assume it's JSON compatible and fall back to the [much] slower method if the parse fails? Does that cost too much time/resources?
Re:The Cost of JavaScript in 2019 by baddox on Sunday June 30, 2019 @05:04PM (#20320506)
Isn’t that effectively what a backtracking parser does? My naive guess is that’s already roughly how the JS parser works, and that process is what makes it slower than the simpler JSON parser.
Re:The Cost of JavaScript in 2019 by daleharvey on Sunday June 30, 2019 @03:10PM (#20319876)
That would make all JavaScript that has non JSON object literals (most JS code) slower
Re:The Cost of JavaScript in 2019 by pbhjpbhj on Sunday June 30, 2019 @03:33PM (#20320011)
How much slower?

We already established it's far slower parsing js; in most cases JSON parsing would fail - IIRC - on the first character ^[^{] ... so would it be worth being slower by the time it takes to check the first character, and then only JavaScript that started with { would be slower. Which I guess is virtually none.

I suppose those sorts of questions are part of what makes language design interesting.

Re:The Cost of JavaScript in 2019 by kiliancs on Sunday June 30, 2019 @04:55PM (#20320456)
While it's possible that there's room for improvement, it makes sense that parsing a subset of JS (JSON) is faster than parsing JS. JSON is just so much simpler.