bulk operations or iterators

Benchmark created by Kyle Simpson on


Description

Testing/profiling the code patterns from the Script Junkie article "(pre)Maturely Optimize Your JavaScript"

http://msdn.microsoft.com/en-us/scriptjunkie/gg622887.aspx

Snippet comparison #2

Preparation HTML

<script>
  function myData_1() {
   var idx = 0,
       data = [];
  
   this.next = function() {
    return data[idx++];
   };
   this.add = function(val) {
    data[data.length] = val;
   };
   this.size = function() {
    return data.length;
   };
  }
  
  function myData_2() {
   var idx = 0,
       data = [];
  
   this.size = 0;
   this.next = function(num) {
    if (num) {
     return data.slice(idx, idx += num);
    }
    return data[idx++];
   };
   this.add = function(val) {
    if (Object.prototype.toString.call(val) == "[object Array]") {
     data = data.concat(val);
     this.size = data.length;
    }
    else {
     data[data.length] = val;
     this.size++;
    }
   };
  }
  
  var obj_1, obj_2, i, tmp_list = [],
      tmp_ret;
  
  for (i = 0; i < 1000; i++) {
   tmp_list.push(Math.random());
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
iterators
obj_1 = new myData_1();

for (i = 0; i < 1000; i++) {
 obj_1.add(tmp_list[i]);
}

for (i = 0; i < obj_1.size(); i++) {
 obj_1.next();
}
ready
bulk operations
obj_2 = new myData_2();

obj_2.add(tmp_list);

tmp_ret = obj_2.next(obj_2.size);
for (i = 0; i < tmp_ret.length; i++) {
 tmp_ret[i];
}
ready

Revisions

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

  • Revision 1: published by Kyle Simpson on