weakmaps-vs-arrays

JavaScript performance comparison

Test case created by WebReflection

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.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array
result = createItems(1000);
thousand = result.map(getArr);
spliceArr(thousand.length);
hundred = result.slice(0, 100).map(getArr);
spliceArr(hundred.length);
thousand = result.map(getArr);
spliceArr(thousand.length);
pending…
WeakMap
result = createItems(1000);
thousand = result.map(getWM);
hundred = result.slice(0, 100).map(getWM);
thousand = result.map(getWM);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments