Last Updated: September 27, 2021
·
1.986K
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside