functional wrappers vs loops

JavaScript performance comparison

Test case created by Tommy Etkin

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var buffers = [], i, j, b;
   
    for(i = 0; i < 300; i++)
    {
      b=[];
      for(j=0;j<i%17;j++) b.push(j);
      buffers.push(b);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
basic loop
var total=0, bufcount = buffers.length, x;
for(x = 0; x < bufcount; x++)
{
  total += buffers[x].length;
}
 
pending…
functional wrappers
function add(a,b){return a+b;}
function prop(name){ return function(o){return o[name];}; }
var total = buffers.map(prop('length')).reduce(add);
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

Add a comment