sid

JavaScript performance comparison

Test case created by Mark Krueger

Preparation code

    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js">
    </script>
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js">
      
<script>
Benchmark.prototype.setup = function() {
      var count = 500;
      var stationIds = [];
      var models = new Backbone.Collection();
      /*
  
      */
  
      function randString() {
          var s = "";
          for (var i =0 ; i < 10; i++) {
              s += Math.floor(Math.random() * 10).toString();
          }
          return s;
      }
  
      function prep() {
          for (var i=0; i < count; i++) {
              var sid = randString();
              if (Math.random() > 0.2) {
                  stationIds.push(sid);
              }
              var model = new Backbone.Model({stationId:sid, foo:"bar",bar:"foo",id:i});
              models.add(model);
              if (Math.random() < 0.2) {
                  var m2 = new Backbone.Model({stationId:randString(), foo:"bar",bar:"foo",id:i});
                  models.add(m2);
              }
          }
          stationIds = stationIds.sort(function(a,b) { return a-b;});
      }
  prep();

};
</script>

Preparation code output

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js">

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
previous
var added = [];
        var stids = stationIds.slice(0);
        stids.forEach(stationId => {
            if (models.where({ stationId}).length == 0) {
                added.push(stationId);
            }
        });
        return added;
pending…
barnaby
var added = []

        var stids = stationIds.slice(0);
        var stationIdsLength = stids.length;
        let modelStationIds  = models.reduce(function(acc,cv) {acc[cv.attributes.stationId]=1;return acc;},{});
         for (var  i=0; i < stationIdsLength; i++) {
            var stationId = stids[i];
            if (modelStationIds[stationId]) {   
                added.push(stationId);
            }
        }
pending…
alt
var added = [];
        var stids = stationIds.slice(0);
        stids.forEach(stationId => {
            if (models.findWhere({ stationId:stationId}).length != 0) {
                added.push(stationId);
            }
        });
        return added;
pending…

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

0 Comments