skeeto / resurrect-js

JavaScript serialization that preserves behavior and reference circularity.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warning: "mutating the [[Prototype]] of an object will cause your code to run very slowly;"

ZombieProtectionAgency opened this issue · comments

commented

I don't know enough about javascript objects/prototypes to know if this is a real issue and I dont know if you already knew about this but just in case...

mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create

I just noticed this warning in the console of Firefox 39.0. The error points to line #483, in fixPrototype.

Just from reading a few questions/issues on Stackoverflow/Github it seems like the issue is with the fact that newer Javascript engines optimize objects on creation and manually overriding proto undoes that.

The problem is that the object is created by JSON.parse with no
opportunity to use Object.create. Take a look at the "else" clause just
below the linked potion, where it makes a copy using Object.create. This
is a fallback for browsers without support for proto (Internet
Explorer < 11), though now that proto has been standardized in ES6
this need should be diminishing.

There's a trade-off at play here, but I don't have any benchmarks to
know the actual costs involved. The way it's written now, deserializing
is faster because a copy isn't made, but resurrected methods will have
extra overhead due to proto meddling. I could drop the use of
proto and always create a copy. This would slow down serialization,
but the copy would have better method performance.

I guess ultimately this should be a configuration option, just like
"cleanup" is an option. But I don't know if I want to bother with it
until I see some performance benchmarks that show it could matter.