Deep cloning objects defined by JSON.

Allen Wirfs-Brock 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).

So why should we expect a "native" deep clone function to be significantly faster than a  JavaScript version of the same function running on a modern high performance  JS engine. After all, a clone basically just does function calls, property lookup and object creation and property creation. These really are the foundation operations of  most data structure intensive applications.  If a pure JS deep clone is too slow then many other data structure driven functions are also going to be too slow.  If, in fact, a pure JS implementation of deep clone on an optimizing engine is still significantly slower than a native code implementation on the same engine then  perhaps we would be better served to focus on eliminating the bottlenecks that slow down the JS version of deep clone instead for putting the effort into creating an native version of that particular function.

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. 

Allen







More information about the es-discuss mailing list