Last Updated: September 27, 2021
·
1.753K
· maikudou

Effective deep copy of javaScript objects

Sometimes one needs to copy a js object without any possible links to other objects, so keep it in mind:

newObj = JSON.parse(JSON.stringify(obj)) 

is 4 (four) times faster than

newObj = jQuery.extend(true, {}, obj)

Unfortunately it's inappropriate for objects with functions with it

3 Responses
Add your response

It seems it really depends on object size. It would be cool if jsperf had an option to map a variable to an axis (i.e. object size as 'x').

http://jsperf.com/dipcopy

over 1 year ago ·