Native Array Sort Performance vs. Backbone Sort (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script>


<script>
  var models = _.shuffle(_.times(20, function(i) {
    return i ;
  }));


  var sortNumber = function(a,b){
    return a-b;
  };

</script>

Setup

var collectionA = [];        
    var collectionB = new Backbone.Collection();
    collectionB.comparator = 'id';

Teardown


    collectionA = [];
    collectionB.reset();
    
  

Test runner

Ready to run.

Testing in
TestOps/sec
Sort Once Native
collectionA = [];
models.forEach(function(element, index, array){
  collectionA.push(element);
});

collectionA.sort(sortNumber)
 
ready
Sort Each Element Native
collectionA = [];
models.forEach(function(element, index, array){
  collectionA.push(element);
  collectionA.sort(sortNumber)
});


 
ready
Backbone Sort Each
models.forEach(function(element, index, array){
  collectionB.add({id:element});
});
ready
Backbone Sort Once
models.forEach(function(element, index, array){
  collectionB.add({id:element}, {sort:false});
});

collectionB.sort();
ready

Revisions

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