patternjavascriptMinor
Deep clone method
Viewed 0 times
deepclonemethod
Problem
I'm quite new to JavaScript and wrote this clone method to be able to clone any kind of object. It works quite well for the cases I've tested until now, but for JSON objects it seemed kind of slow to me. I'm sure there is some additional speed to to squeeze out and a lot of overall improvement to make. But I don't know enough about JavaScript to optimize this.
```
Object.prototype.clone = function(deep, falseArray/check/) {
var type = Object.prototype.toString.call(this).match(/^\[object (.+?)\]$/)[1];
/*var falseArray;
if(check) {
falseArray = (Object.keys(this).length - this.length);
}*/ //takes around 400ms additional at 500k array to check if it has additional properties, just pass it as argument decide if oy
switch (type) {
case "Array" :
var clone = [];
if (!falseArray) {
if (!deep)
clone = this.concat();
else
this.forEach(function(e) {
if(typeof e !== "undefined" && e !== null)
clone.push((typeof e !== "object"?e:e.clone((typeof deep == "boolean"?deep:(deep-1)))));
else
clone.push("");
});
} else {// Variable is an 'Array' but has an extra propertie e.g: var arr = [1,2,3]; arr.a = "b" //its the slowest possibility but normally Objects would be used
for (var prop in this ) {
clone[prop] = this[prop];
}
}
break;
case "Object":
var clone = {};
if (!deep) {
for (var prop in this) {
clone[prop] = this[prop];
}
} else {
for (var prop in this) {
if(typeof this[prop] !== "undefined" && this[prop]!== null)
clone[prop] = (typeof this [prop] !== "object"?this[prop]:this[prop].clone((typeof deep == "boolean"?deep:(deep-1))));
else
clone[prop] = "";
```
Object.prototype.clone = function(deep, falseArray/check/) {
var type = Object.prototype.toString.call(this).match(/^\[object (.+?)\]$/)[1];
/*var falseArray;
if(check) {
falseArray = (Object.keys(this).length - this.length);
}*/ //takes around 400ms additional at 500k array to check if it has additional properties, just pass it as argument decide if oy
switch (type) {
case "Array" :
var clone = [];
if (!falseArray) {
if (!deep)
clone = this.concat();
else
this.forEach(function(e) {
if(typeof e !== "undefined" && e !== null)
clone.push((typeof e !== "object"?e:e.clone((typeof deep == "boolean"?deep:(deep-1)))));
else
clone.push("");
});
} else {// Variable is an 'Array' but has an extra propertie e.g: var arr = [1,2,3]; arr.a = "b" //its the slowest possibility but normally Objects would be used
for (var prop in this ) {
clone[prop] = this[prop];
}
}
break;
case "Object":
var clone = {};
if (!deep) {
for (var prop in this) {
clone[prop] = this[prop];
}
} else {
for (var prop in this) {
if(typeof this[prop] !== "undefined" && this[prop]!== null)
clone[prop] = (typeof this [prop] !== "object"?this[prop]:this[prop].clone((typeof deep == "boolean"?deep:(deep-1))));
else
clone[prop] = "";
Solution
When you say "big JSON objects", do you mean a string containing JSON?
If I were you, I'd compare your method with serializing to a JSON string and deserializing from that string (that's cloning, too). As it is probably implemented in native code and directly supported by the VM, it should be really fast. I expect it to beat anything you can write - and it's way more compact, too. But I didn't test it, YMMV. Just consider and benchmark it.
If I were you, I'd compare your method with serializing to a JSON string and deserializing from that string (that's cloning, too). As it is probably implemented in native code and directly supported by the VM, it should be really fast. I expect it to beat anything you can write - and it's way more compact, too. But I didn't test it, YMMV. Just consider and benchmark it.
Context
StackExchange Code Review Q#13594, answer score: 2
Revisions (0)
No revisions yet.