timeit.js
/*
 * run the function f nloops times, and report on its performance
 */
run the function f nloops times, and report on its performance
run the function f nloops times and report on its performance
function timeit( nloops, f, msg, callback ) {
    var __i, __fn = (typeof f === 'function' ? f : makeFunction(f));
    var __t1, __t2, __callCount = 0;
    var __nleft;
    var __stopTime, __timedRun = false;
    if (typeof msg === 'function') { callback = msg; msg = undefined; }
    if (nloops !== null && nloops <= 0) {
        if (callback) callback(null, 0, 0, 0);
        return { count: 0, elapsed: 0, wallclock: 0 };
    }
    try { } catch (e) { }
    if (__timerOverhead === undefined && !__calibrating) {
        calibrate();
        calibrate();
    }
    function maybeCalibrateCb(cb) {
        if (!__calibrating) calibrateCb(nloops, cb);
        else cb();
    }
    var elseBlock;
    if (!callback) {
        __fn();
        nloops = calibrateLoopCount(nloops, __fn);
        __t1 = fptime();
        __stopTime += __t1;
        for (__i=0; __i<nloops; __i+=5) {
            __callCount += 5;
            __fn(); __fn(); __fn(); __fn(); __fn();
            if ((__callCount & 0xFFF) === 0 && __timedRun && fptime() >= __stopTime) break;
        }
        __t2 = fptime();
        var __duration = (__t2 - __t1 - __timerOverhead - (__loopOverhead * __callCount * 0.000001));
        if (__timedRun) {
            var timedRunOverhead = (Math.floor(__callCount / 4096)) * __timerOverhead;
            __duration -= timedRunOverhead;
        }
        if (msg !== '/* NOOUTPUT */') reportit(f, __callCount, __duration, (__t2 - __t1), msg ? msg : "");
        return {count: __callCount, elapsed: __duration, wallclock: __t2 - __t1 };
    }
    else (elseBlock = function(){
        var __t1, __depth;
        maybeCalibrateCb(function() {
            calibrateLoopCount(nloops, __fn, function(err, ret) {
                nloops = ret;
                __fn( function() {
                    __nleft = nloops;
                    __depth = 0;
                    __t1 = fptime();
                    __launchNext();
                    try { } catch(e) { }
                });
                try { } catch(e) { }
            });
        });
        function __launchNext() {
            if (__nleft) {
                __nleft -= 1;
                __depth += 1;
                __callCount += 1;
                __fn(__onTestDone);
            }
            else {
                __t2 = fptime();
                var __duration = (__t2 - __t1 - (__timerOverhead > 0 ? __timerOverhead : 0) - (__loopOverheadCb * __callCount * 0.000001));
                if (__timedRun) {
                    var timedRunOverhead = (Math.floor(__callCount / 4096)) * __timerOverhead;
                    __duration -= timedRunOverhead;
                }
                if (msg !== '/* NOOUTPUT */') reportit(f, __callCount, __duration, (__t2 - __t1), msg ? msg : "");
                callback(null, __callCount, __duration, __t2 - __t1);
            }
            try { } catch(e) { }
        }
        function __onTestDone() {
            if (__depth > 20) { __depth = 0; setImmediate(__launchNext); }
            else __launchNext();
            try { } catch(e) { }
        }
    })();
}
function timeit ( nloops , f , msg , callback ) { var __i , __fn = ( typeof f === 'function' ? f : makeFunction ( f ) ) ; var __t1 , __t2 , __callCount = 0 ; var __nleft ; var __stopTime , __timedRun = false ; if ( typeof msg === 'function' ) { callback = msg ; msg = undefined ; } if ( nloops !== null && nloops <= 0 ) { if ( callback ) callback ( null , 0 , 0 , 0 ) ; return { count : 0 , elapsed : 0 , wallclock : 0 } ; } try { } catch ( e ) { } if ( __timerOverhead === undefined && ! __calibrating ) { calibrate ( ) ; calibrate ( ) ; } function maybeCalibrateCb ( cb ) { if ( ! __calibrating ) calibrateCb ( nloops , cb ) ; else cb ( ) ; } var elseBlock ; if ( ! callback ) { __fn ( ) ; nloops = calibrateLoopCount ( nloops , __fn ) ; __t1 = fptime ( ) ; __stopTime += __t1 ; for ( __i = 0 ; __i < nloops ; __i += 5 ) { __callCount += 5 ; __fn ( ) ; __fn ( ) ; __fn ( ) ; __fn ( ) ; __fn ( ) ; if ( ( __callCount & 0xFFF ) === 0 && __timedRun && fptime ( ) >= __stopTime ) break ; } __t2 = fptime ( ) ; var __duration = ( __t2 - __t1 - __timerOverhead - ( __loopOverhead * __callCount * 0.000001 ) ) ; if ( __timedRun ) { var timedRunOverhead = ( Math . floor ( __callCount / 4096 ) ) * __timerOverhead ; __duration -= timedRunOverhead ; } if ( msg !== '/* NOOUTPUT */' ) reportit ( f , __callCount , __duration , ( __t2 - __t1 ) , msg ? msg : "" ) ; return { count : __callCount , elapsed : __duration , wallclock : __t2 - __t1 } ; } else ( elseBlock = function ( ) { var __t1 , __depth ; maybeCalibrateCb ( function ( ) { calibrateLoopCount ( nloops , __fn , function ( err , ret ) { nloops = ret ; __fn ( function ( ) { __nleft = nloops ; __depth = 0 ; __t1 = fptime ( ) ; __launchNext ( ) ; try { } catch ( e ) { } } ) ; try { } catch ( e ) { } } ) ; } ) ; function __launchNext ( ) { if ( __nleft ) { __nleft -= 1 ; __depth += 1 ; __callCount += 1 ; __fn ( __onTestDone ) ; } else { __t2 = fptime ( ) ; var __duration = ( __t2 - __t1 - ( __timerOverhead > 0 ? __timerOverhead : 0 ) - ( __loopOverheadCb * __callCount * 0.000001 ) ) ; if ( __timedRun ) { var timedRunOverhead = ( Math . floor ( __callCount / 4096 ) ) * __timerOverhead ; __duration -= timedRunOverhead ; } if ( msg !== '/* NOOUTPUT */' ) reportit ( f , __callCount , __duration , ( __t2 - __t1 ) , msg ? msg : "" ) ; callback ( null , __callCount , __duration , __t2 - __t1 ) ; } try { } catch ( e ) { } } function __onTestDone ( ) { if ( __depth > 20 ) { __depth = 0 ; setImmediate ( __launchNext ) ; } else __launchNext ( ) ; try { } catch ( e ) { } } } ) ( ) ; }
run the function f nloops times, and report on its performance
run the function f nloops times and report on its performance
// disable optimization of this function.  Its overhead is subtracted out, // and optimization would make the overhead less predictable. // calibrate, then use the measured overhead to re-calibrate more accurately // calibrate unless already calibrating // node v0.11.x strongly penalizes parsing the function in the timed loop; v0.10 did not. // Run the test function once to pre-parse it // timed test begins here, called after __fn has been precompiled // disable optimization in the test invoker, to make overhead more stable // results are unrealistically low (by 50%) if not in try-catch