ES6 Map vs Object properties

JavaScript performance comparison

Revision 212 of this test case created by

Preparation code

<script>
function randomString()
{
text = Math.floor(Math.random()*1024);

    return text;
}

var preComputedRandomKeys = [];
var preComputedRandomVals = [];

for (var i=0; i < 100000; i++) {
   preComputedRandomKeys[i] = randomString();
   preComputedRandomVals[i] = randomString();
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var arr = new Array();
  var obj = {};
  
  var arr2 = new Array();
  var obj2 = {};
  
  for (var i=0; i < 100000; i++) {
     arr2[preComputedRandomKeys[i]] = preComputedRandomVals[i];
     obj2[preComputedRandomKeys[i]] = preComputedRandomVals[i];
  }
  
  for (var i=0; i < 100000; i++) {
     arr2[preComputedRandomKeys[i]] = arr2[preComputedRandomKeys[i]];
     obj2[preComputedRandomKeys[i]] = obj2[preComputedRandomKeys[i]];
  }

};
</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
Iterate Object
var key;
for (key in obj2) {
  if (obj2.hasOwnProperty(key))
    var val = obj2[key];
}
pending…
Iterate Map
var valx;
for (var [key, val] of arr.entries()) {
  valx = val;
}
pending…
val = map.get(key)
for (var i=0; i < 100000; i++) {
   var val = arr2[preComputedRandomKeys[i]];
}
pending…
val = obj[key]
for (var i=0; i < 100000; i++) {
   var val = obj2[preComputedRandomKeys[i]];
}
pending…
map.set(key,val)
for (var i=0; i < 100000; i++) {
   var key = preComputedRandomKeys[i];
   var val = preComputedRandomVals[i];
   arr[key] = val;
}
pending…
obj[key] = val;
for (var i=0; i < 100000; i++) {
   var key = preComputedRandomKeys[i];
   var val = preComputedRandomVals[i];
   obj[key] = val;
}
pending…

Compare results of other browsers

Revisions

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

0 Comments