Dict vs Flattened lookup

JavaScript performance comparison

Test case created by Joshua Inkenbrandt

Info

Wanted to compare a flattened lookup of keys (A single array with all a users collection keys in it) to a dictionary lookup (An object that separates keys by their type).

Example: flat = ['t1'...'a999']; dict = {'t':[1...999], 'a':[1...999]};

Preparation code

<script>
  var types = ['t', 'ot', 'p', 'op', 'or', 'oa'];
  var dict = {};
  var flat = [];
 
  var collectionSize = 10000;
 
  for (var i = 0; i < collectionSize; i++) {
    var t = types[Math.round(Math.random() * types.length)];
    dict[t] = dict[t] || [];
    dict[t].push(i);
    flat.push(t + i);
  }
 
  dict['t'].push(999);
  flat.push('t999');
</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
Dict lookup of track 999
dict['t'].indexOf(999) != -1
pending…
Flat lookup of track 999
flat.indexOf('t999') != -1
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