Finding element: Object vs Map vs Array

Benchmark created by tswistak on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.6.9/core.min.js" integrity="sha256-+TULExmyD/NYybiq1jR6qIxiCQHqmjb3yGVZyKyIgso=" crossorigin="anonymous"></script>

Setup

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);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Array, find
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = array.find(function(current) { return current.key === keyToFind; });
ready
Array, filter
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = array.filter(function(current) { return current.key === keyToFind; })[0];
ready
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;
    }
}
ready
Object
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = object[keyToFind];
ready
Map
var keyToFind = ''+(Math.floor(Math.random()*1000000));
var element = map.get(keyToFind);
ready

Revisions

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