callback vs iterator

Benchmark created by Roan Kattouw on


Setup

var data = [], i;
    for ( i = 0; i < 1000; i++ ) data[i] = i;
    
    Traverser = function( arr ) {
        this.a = arr;
        this.nextIndex = 0;
    };
    Traverser.prototype.next = function() {
        return this.nextIndex < this.a.length ? this.a[this.nextIndex++] : null;
    };
    Traverser.withCallback = function( arr, callback ) {
        for ( var j = 0 ; j < arr.length; j++ ) {
            callback( arr[j], j );
        }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
callback
var cbSum = 0;
Traverser.withCallback( data, function( v ) { cbSum += v; } );
 
ready
traverser
var trvSum = 0, t = new Traverser( data ), n;
while ( (n = t.next()) !== null ) { trvSum += n; }
 
ready
unrolled
var unrSum = 0, k;
for ( k = 0; k < data.length; k++ ) { unrSum += data[k]; }
ready

Revisions

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

  • Revision 1: published by Roan Kattouw on