Compare while loop vs for loop

JavaScript performance comparison

Revision 11 of this test case created by

Preparation code

 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  // first create a big array o' stuff
  var BigArrayOStuff = [];
  for (var j = 0; j < 10; j++) {
    var dude = Object.create(null);
    dude.RequirementName = "this is a test";
    dude.Comment = "this is a test";
    dude.NumberOfPresentations = "this is a test";
    dude.RequirementId = "this is a test";
    dude.CreateDate = "this is a test";
    dude.UpdateDate = "this is a test";
    dude.ProductId = "this is a test";
    dude.QueryCategoryId = "this is a test";
    BigArrayOStuff.push(dude);
  }
  
  
  // Constructor for requirement
  var Req = function(options) {
    this.name = options.name;
    this.details = options.details;
    this.presentations = []
    this.numOfPresentations = options.numOfPresentations; // doing double duty: whether to show a "plus" icon on tree, and whether we have already loaded presentations
    this.id = options.id;
    this.clientId = options.clientId;
    this.createdDate = options.createdDate;
    this.updatedDate = options.updatedDate;
    this.productId = options.productId;
    this.queryCategoryId = options.queryCategoryId;
    this.displayPresentations = false
    this.isAnalytic = this.queryCategoryId === 3; // whether to show the little chart icon
  };

};
</script>

Preparation code output

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
underscore test
var items = _.map(BigArrayOStuff, function(item) {
    return new Req({
      name: item.RequirementName,
      details: item.Comment,
      numOfPresentations: item.NumberOfPresentations,
      id: item.RequirementId,
      clientId: 2,
      createdDate: item.CreateDate,
      updatedDate: item.UpdateDate,
      productId: item.ProductId,
      queryCategoryId: item.QueryCategoryId
    });
pending…
native code for loop
var temporaryArray = [];
for (var i = 0; i < BigArrayOStuff.length; i++) {
  var options = {};
  options.name = BigArrayOStuff[i].RequirementName;
  options.details = BigArrayOStuff[i].Comment;
  options.numOfPresentations = BigArrayOStuff[i].NumberOfPresentations;
  options.id = BigArrayOStuff[i].RequirementId;
  options.clientId = 1;
  options.createdDate = BigArrayOStuff[i].CreateDate;
  options.updatedDate = BigArrayOStuff[i].UpdateDate;
  options.productId = BigArrayOStuff[i].ProductId;
  options.queryCategoryId = BigArrayOStuff[i].QueryCategoryId;
  var myRequirement = new Req(options);
  temporaryArray.push(myRequirement);
}
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.

0 Comments