loop vs linear

JavaScript performance comparison

Test case created by 2x2p

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var triads = []
  while(triads.length<10000){
    triads.push([1,2,3]);
  }
  
  var objects = []
  while(objects.length<10000){
    objects.push({a:1,b:2,c:3});
  }
  
  function triSubLoop(_tri){
    for (var t = 0, len = _tri.length; t < len; t++) {
      _tri[t] = _tri[t]/2;
    }
    return _tri;
  }
  
  function objSubLoop(_obj){
    for (var k in _obj) {
      if (_obj.hasOwnProperty(k)) {
        _obj[k] = _obj[k]/2;
      }
    }
    return _obj;
  }
  
  function triLinear(_tri){
    _tri[0] = _tri[0]/2;
    _tri[1] = _tri[1]/2;
    _tri[2] = _tri[2]/2;
    return _tri;
  }
  
  function objLinear(_obj){
    _obj.a = _obj.a/2;
    _obj.b = _obj.b/2;
    _obj.c = _obj.c/2;
    return _obj;
  }

};
</script>

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
triads in loop
var result = [];
for (var i = 0, len = triads.length; i < len; i++) {
  result[i] = triSubLoop(triads[i]);
}
pending…
objects in loop
var result = [];
for (var i = 0, len = objects.length; i < len; i++) {
  result[i] = objSubLoop(objects[i]);
}
pending…
triads linear
var result = [];
for (var i = 0, len = triads.length; i < len; i++) {
  result[i] = triLinear(triads[i]);
}
pending…
objects linear
var result = [];
for (var i = 0, len = objects.length; i < len; i++) {
  result[i] = objLinear(objects[i]);
}
pending…

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

Compare results of other browsers

0 Comments