Last active
July 29, 2019 15:18
-
-
Save warpech/45dbae3ed7f738d1de8e841b0f5aa43f to your computer and use it in GitHub Desktop.
Deep freeze copy vs JSON.parse/stringify (http://jsbench.github.io/#45dbae3ed7f738d1de8e841b0f5aa43f) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Deep freeze copy vs JSON.parse/stringify</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
"use strict"; | |
// based on https://github.com/porsager/deep-freeze-copy | |
function deepFreezeCopy(o) { | |
return Object.freeze( | |
Object.keys(o).reduce((acc, key) => ( | |
o[key] instanceof Date | |
? new Date(o[key]) | |
: acc[key] = typeof o[key] === 'object' && o[key] !== null | |
? deepFreezeCopy(o[key]) | |
: o[key], | |
acc | |
), Array.isArray(o) ? [] : {}) | |
) | |
} | |
function deepFreezeCopy2(o) { | |
return Object.freeze( | |
Object.keys(o).reduce((acc, key) => ( | |
acc[key] = typeof o[key] === 'object' && o[key] !== null | |
? deepFreezeCopy2(o[key]) | |
: o[key], | |
acc | |
), Array.isArray(o) ? [] : {}) | |
) | |
} | |
function deepFreezeCopy3(orig) { | |
const tmp = Object.assign(Array.isArray(orig) ? [] : {}, orig); | |
const keys = Object.keys(tmp); | |
for (let i = 0; i < keys.length; i++) { | |
if (typeof tmp[keys[i]] === 'object' && tmp[keys[i]] !== null) { | |
tmp[keys[i]] = deepFreezeCopy3(tmp[keys[i]]); | |
} | |
} | |
return tmp; | |
} | |
function deepFreezeCopy4(orig) { | |
const tmp = Array.isArray(orig) ? [] : {}; | |
const keys = Object.keys(orig); | |
for (let i = 0; i < keys.length; i++) { | |
if (typeof orig[keys[i]] === 'object' && orig[keys[i]] !== null) { | |
tmp[keys[i]] = deepFreezeCopy4(orig[keys[i]]); | |
} | |
else { | |
tmp[keys[i]] = orig[keys[i]] | |
} | |
} | |
return tmp; | |
} | |
function deepFreezeCopy5(orig) { | |
const tmp = Array.isArray(orig) ? [] : {}; | |
const keys = Object.keys(orig); | |
for (let i = 0; i < keys.length; i++) { | |
const key = keys[i]; | |
const val = orig[key]; | |
if (typeof val === 'object' && val !== null) { | |
tmp[key] = deepFreezeCopy5(val); | |
} | |
else { | |
tmp[key] = val; | |
} | |
} | |
return tmp; | |
} | |
function deepFreezeCopy6(orig) { | |
const tmp = Array.isArray(orig) ? [] : {}; | |
for (let key in orig) { | |
if (orig.hasOwnProperty(key)) { | |
const val = orig[key]; | |
if (typeof val === 'object' && val !== null) { | |
tmp[key] = deepFreezeCopy6(val); | |
} | |
else { | |
tmp[key] = val; | |
} | |
} | |
} | |
return tmp; | |
} | |
function deepFreezeCopy7(orig) { | |
switch (typeof orig) { | |
case 'object': | |
if (orig === null) { | |
return orig; | |
} | |
const tmp = Array.isArray(orig) ? [] : {}; | |
const keys = Object.keys(orig); | |
for (let i = 0; i < keys.length; i++) { | |
tmp[keys[i]] = deepFreezeCopy7(orig[keys[i]]); | |
} | |
return tmp; | |
case 'undefined': | |
return null; //this is how JSON.stringify behaves for array items | |
default: | |
return orig; //no need to clone primitives | |
} | |
} | |
var obj = { | |
people: [ | |
{ name: "Albert" } | |
] | |
} | |
var frozen = deepFreezeCopy(obj); | |
obj.people[0].name = "Joachim"; | |
console.log("____", obj.people[0].name, frozen.people[0].name); | |
}; | |
suite.add("JSON.parse(JSON.stringify(obj));", function () { | |
JSON.parse(JSON.stringify(obj)); | |
}); | |
suite.add("deepFreezeCopy(obj);", function () { | |
deepFreezeCopy(obj); | |
}); | |
suite.add("deepFreezeCopy2(obj);", function () { | |
deepFreezeCopy2(obj); | |
}); | |
suite.add("deepFreezeCopy3(obj);", function () { | |
deepFreezeCopy3(obj); | |
}); | |
suite.add("deepFreezeCopy4(obj);", function () { | |
deepFreezeCopy4(obj); | |
}); | |
suite.add("deepFreezeCopy5(obj);", function () { | |
deepFreezeCopy5(obj); | |
}); | |
suite.add("deepFreezeCopy6(obj);", function () { | |
deepFreezeCopy6(obj); | |
}); | |
suite.add("deepFreezeCopy7(obj);", function () { | |
deepFreezeCopy7(obj); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Deep freeze copy vs JSON.parse/stringify"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment