Deep cloning objects defined by JSON.
allen at wirfs-brock.com
Sun Jan 22 16:34:55 PST 2012
On Jan 22, 2012, at 11:58 AM, Jake Verbaten wrote:
> Most of the time the purpose of deep cloning objects is deep cloning data structures.
> It's been discussed that generically deep cloning proxies, privates and functions is a non-trivial problem.
> However it would be of value to have a mechanism to deep clone anything that would be valid JSON (Limiting to JSON is arbitary, it's a well defined subset and none of the subset would involve difficult points to resolve).
The following is just speculation...One possible such bottleneck might be whole object allocation. A JS clone function probably would have to allocate an empty object and then dynamically populate it by adding properties one at a time. A native implementation is more like to have the ability to examine a complete object and create, in a single primitive operation, a new object with all of the same properties as the original object. In other words, a native implementation of deep clone is likely to use some sort of shallow clone operation is that not available to pure JS code. This suggest that a better way to get faster deep cloning functions is to make a native shallow clone function available to JS code.
More information about the es-discuss