weakmaps-vs-arrays
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var wm = new WeakMap;
function getWM(item) {
return wm.get(item) || setWM(item);
}
function setWM(item) {
var value = Math.random() * item.id;
wm.set(item, value);
return value;
}
var arr = [];
function getArr(item, index) {
return arr[index] || setArr(item, index);
}
function setArr(item, index) {
var value = Math.random() * item.id;
arr[index] = value;
return value;
}
function spliceArr(length) {
arr.splice(length);
}
function createItems(length) {
var items = [];
for (var i = 0; i < length; i++) {
items[i] = {id: 1 + parseInt(Math.random() * length)};
}
return items;
}
var result = [];
var thousand = [];
var hundred = [];
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
Array
|
|
pending… |
WeakMap
|
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
0 Comments