Skip to content

Instantly share code, notes, and snippets.

@Ely-S
Created December 2, 2012 22:55
Show Gist options
  • Select an option

  • Save Ely-S/4191458 to your computer and use it in GitHub Desktop.

Select an option

Save Ely-S/4191458 to your computer and use it in GitHub Desktop.
Fastest way to Deep Clone/Extend an object in javascript
//benchmark available here
// clone is the object to recursively copy the attributes of obj to overwriting as necessary.
function x(clone, obj) {
for(var i in obj)
clone[i] = (typeof obj[i] == "object") ? x(obj[i].constructor(), obj[i]) : obj[i];
return clone;
}
@tbjgolden
Copy link

or even (obj[i] && typeof obj[i] == "object")

this ends up being 3 times faster than JSON.parse(JSON.stringify(obj)) and is barely more code 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment