Created
December 21, 2020 09:24
-
-
Save wooandoo/038f2e9cc1f715afa8ee89de7e02983e to your computer and use it in GitHub Desktop.
shallow copy of object in different sizes (http://jsbench.github.io/#038f2e9cc1f715afa8ee89de7e02983e) #jsbench #jsperf
This file contains hidden or 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>shallow copy of object in different sizes</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 hidden or 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 () { | |
const init_object = (property_count) => { | |
const object = {} | |
for (let index = 0; index < property_count; index++) { | |
object[`property_${index}`] = {} | |
} | |
return object | |
} | |
const tiny_object = init_object(1) | |
const small_object = init_object(10) | |
const large_object = init_object(100) | |
const big_object = init_object(10000) | |
const copy_object = (object) => ({ ...object }) | |
}; | |
suite.add("copy_object(tiny_object)", function () { | |
copy_object(tiny_object) | |
}); | |
suite.add("copy_object(small_object)", function () { | |
copy_object(small_object) | |
}); | |
suite.add("copy_object(large_object)", function () { | |
copy_object(large_object) | |
}); | |
suite.add("copy_object(big_object)", function () { | |
copy_object(big_object) | |
}); | |
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("shallow copy of object in different sizes"); | |
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