Angelo
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
Probably slow.
> var clone = {};
> Object.getOwnPropertyNames(o).forEach(function (name) {
> clone[name] = o[name];
> });
Wrong, that's a shallow copy.
> > var clone = JSON.parse(JSON.stringify(o));Doesn't work with dates and functions. Use _.clone.
> var clone = JSON.parse(JSON.stringify(o));
Probably slow.
Wrong, that's a shallow copy.
> var clone = {};
> Object.getOwnPropertyNames(o).forEach(function (name) {
> clone[name] = o[name];
> });
> Deep cloning arbitary javascript objects is a difficult problem to solve.It's easy if you use _.clone(). (I'm sorry if I'm a broken record but nobody seems to take underscore seriously. It's the second most-used module.)
> Wrong, requiring deep clones is a data structure design mistake.That's silly. If you need a deep clone then you need a deep clone. I use them all the time.
if you can point me in the direction of any reading i can
do around that or at some v8 source that might be enlightening, i'd be
very interested.
> var clone = JSON.parse(JSON.stringify(o));
Probably slow.
Hmm. The question is whether you are cloning an object's properties or the actual typed object. I have not run into the need for cloning a typed object. It would be easy to switch prototypes after the clone. It would also be easy to add a flag for this to underscore.
> Note when I say "arbitary objects" I also mean "How do you clone proxies, closures and private state?"
I'd love to have access to closures. Is that being considered for future JS specs? For now I have to live with plain objects since I only write code that works on both the server and client where possible. I find it too hard to remember which environment I'm in and what is allowed.
Streaming JSON libs are several times slower. Just do a recursive copy.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mai...
i'd be surprised if you ever have a genuine reason to
perform a deep clone
Oh, that'd be awesome. :)