Test name | Executions per second |
---|---|
deepclone | 167270352.0 Ops/sec |
JSON Parse | 120818824.0 Ops/sec |
Object.assign | 166235760.0 Ops/sec |
function deepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
const clone = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
clone[key] = deepClone(obj[key]);
}
}
return clone;
}
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while (n.length < 1000) {
n = deepClone(n);
}
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while(n.length < 1000) {
n = JSON.parse(JSON.stringify(n))
}
var n = {foo: 'bar', hello: [{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'},{hello: 'world', foo: 'bar'}]};
while(n.length < 1000) {
n = Object.assign({}, n);
}