Map vs Object vs frozen

JavaScript performance comparison

Test case created by twothe

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const chars = 'abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'.split('');
  function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
  }
  function getRandomChar() {
    return chars[getRandomInt(chars.length)];
  }
  function word() {
    const size = getRandomInt(15) + 5;
    let result = [];
    for (let i = size; i > 0; --i) {
      result.push(getRandomChar());
    }
    return result.join('');
  }
  
  function genData(size) { 
    const resultObj = {};
    const resultMap = new Map();
    for (let i = size; i>0; --i) {
      const key = word();
      const value = getRandomInt(65536);
      resultObj[key] = value;
      resultMap.set(key, value);
    }
    return {obj: resultObj, map: resultMap};
  }
  
  const { obj, map } = genData(10);
  const coldObj = Object.freeze(obj);
  
  const objGet = function(word) { return obj[word]; }
  const coldObjGet = function(word) { return coldObj[word]; }
  
  var sum = 0;

};

Benchmark.prototype.teardown = function() {
  console.log(sum);

};
</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
Object simple
sum += objGet(word());
pending…
Map simple
sum += map.get(word());
pending…
Object frozen
sum += coldObjGet(word());
pending…
NoOp
sum = 5;
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