"Getting" items test

JavaScript performance comparison

Revision 4 of this test case created

Info

Which one of these is the fastest for getting a record by ID?

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone.js"></script>
<script src="//rawgithub.com/mauriciosantos/buckets/master/buckets-minified.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var NUM_RECORDS = 1000; //one thousand
   
    var array = []; //test1: array
    var obj = {}; //test2: obj by id
   
    //test3: backbone collection
    var CrowdTorch = {};
    CrowdTorch.TestModel = Backbone.Model.extend({});
    CrowdTorch.TestCollection = Backbone.Collection.extend({
      model: CrowdTorch.TestModel
    });
    var collection = new CrowdTorch.TestCollection();
   
    //test 4: buckets dictionary
    var bucketsDict = new buckets.Dictionary();
   
    //insert records into each collection
    for (var i = 0; i < NUM_RECORDS; i++) {
      var id = i;
      var name = "Name " + i;
      var record = {
        "Id": id,
        "Name": name
      };
   
      array.push(record); //array
      obj[id] = record; //object by id
   
      //backbone collection
      var bbRecord = new CrowdTorch.TestModel();
      bbRecord.set(record);
      collection.add(bbRecord);
   
      //buckets dictionary
      bucketsDict.set(id, record);
    }
   
    function lotsOfLookups() {
      for (var i = 0; i < NUM_RECORDS; i++) {
        performLookup(i);
      }
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Array
function performLookup(id) {
  for (var i = 0; i < array.length; i++) {
    var record = array[i];
    if (record.Id == id) {
      result = record;
      break; //we found it!
    }
  }
}

lotsOfLookups();
pending…
Object by Id
function performLookup(id) {
  var result = obj[id];
}

lotsOfLookups();
pending…
Backbone Collections
function performLookup(id) {
 var result = collection.get(id);
}

lotsOfLookups();
pending…
Buckets Dictionary
function performLookup(id) {
  var result = bucketsDict.get(id);
}

lotsOfLookups();
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