Map vs Object as hashes

JavaScript performance comparison

Revision 2 of this test case created by Rahly

Info

This benchmark can just be run on a browser that supports Simple Maps and Sets [http://wiki.ecmascript.org/doku.php?id=harmony:simplemapsand_sets]

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {},
      map = new Map,
      hop = Object.prototype.hasOwnProperty
    for (var i = 0; i < 100000; i++) {
      obj[i] = true
      map.set(i, true)
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
get (Object)
hop.call(obj, 10) ? obj[10] : undefined // true
hop.call(obj, 'ok') ? obj['ok'] : undefined // false
hop.call(obj, 99999) ? obj[99999] : undefined // true
hop.call(obj, 2398413984) ? obj[2398413984] : undefined // false
pending…
get (Map)
map.get(10) // true
map.get('ok') // undefined
map.get(99999) // true
map.get(2398413984) // undefined
pending…
get (Map) 2
map[10];
map['ok'];
map[99999];
map[2398413984];
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. Here’s a list of current revisions for this page:

0 comments

Add a comment