weakmaps-vs-arrays

Benchmark created by WebReflection on


Setup

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 = [];

Test runner

Ready to run.

Testing in
TestOps/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);
ready
WeakMap
result = createItems(1000);
thousand = result.map(getWM);
hundred = result.slice(0, 100).map(getWM);
thousand = result.map(getWM);
ready

Revisions

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

  • Revision 1: published by WebReflection on