Deep cloning objects defined by JSON.

Russell Leggett russell.leggett at gmail.com
Mon Jan 23 13:58:51 PST 2012


On Mon, Jan 23, 2012 at 5:37 AM, Herby Vojčík <herby at mailbox.sk> wrote:

> Allen Wirfs-Brock wrote:
>
>> 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.
>>
>
> +1, nice.
> Well, to see if this is the bottleneck, one needs to benchmark, first.
>
> But I feel the need for shallow clone in the language. Such API should be
> there, and if needed, with native implementation as well.
>
>
As part of the HTML5 specification, there is the structured clone
algorithm<http://www.w3.org/TR/html5/common-dom-interfaces.html#safe-passing-of-structured-data>
 whose
primary use case is web workers. It has already been implemented in Chrome
and Firefox, I believe. Not sure if that might be a good place to start in
possibly exposing that as part of JavaScript.

If I were to make a feature request here, instead of a clone that might not
be terribly useful, I would love to see some better support for Clojure
style persistent data structures <http://clojure.org/data_structures#Data
Structures-Maps (IPersistentMap)>.

I know that's a bit of a long shot, but for all the uses of deep cloning I
can think of, I feel like persistent data structures are much nicer, and
harder to implement in JS.

- Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120123/77666d07/attachment.html>


More information about the es-discuss mailing list