Follow

Did you know: JSON handling is so aggressively optimized in JS runtimes that often, the fastest way to deep-clone an object tree is to do a JSON stringify/parse cycle instead of iterating through the objects in your own code

@joepie91 there was a V8 talk I watched that suggested it was more performant to initialize an object by JSON.parse'ing a string rather than var thing = {} for the same reason.

@joepie91 @rallias that’s fucked up and horrifying but not at all surprising

@Byte @joepie91 I mean, the way they explained it made sense.

If you're parsing a string, all the state machine has to care about at that juncture is backslashes and the end of the string (or $'s, if you're using backtics), and the state machine for JSON parsing is so restricted relative to javascript, the two combined ends up being cheaper than the full mechanism for passing as javascript source.

@joepie91 @rallias I see. Still, it just *feels* wrong, you know?

@joepie91 iirc Chakra (the JS engine used by non-Chrome Edge back in the day) had a special string representation for the result of JSON.stringify, so it didn’t actually generate the string at all if it just got passed back to JSON.parse

@joepie91 ah yea I think Amos touched on this in their recent video

Sign in to participate in the conversation
Pixietown

Small server part of the pixie.town infrastructure. Registration is closed.