Finding element: Object vs Map vs Array

JavaScript performance comparison

Test case created by tswistak

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.6.9/core.min.js" integrity="sha256-+TULExmyD/NYybiq1jR6qIxiCQHqmjb3yGVZyKyIgso=" crossorigin="anonymous"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var array = [];
  var object = {};
  var map = new Map();
  
  for (var i = 0; i < 1000000; i++) {
      var element = {
          key: ''+i,
          value: ''+Math.random()
      };
      array.push(element);
      object[element.key] = element;
      map.set(element.key, element);
  }
  

};
</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, find
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = array.find(function(current) { return current.key === keyToFind; });
pending…
Array, filter
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = array.filter(function(current) { return current.key === keyToFind; })[0];
pending…
Array, for loop
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element;
for (var i = 0; i < array.length; i++) {
    var current = array[i];
    if (current.key === keyToFind) {
        element = current;
        break;
    }
}
pending…
Object
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = object[keyToFind];
pending…
Map
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = map.get(keyToFind);
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