train
Version:
Train a fast (FIFO) queue with a rollback mechanism. Behind the scenes it uses 2 arrays to simulate and perform fast shifting and popping operations without using the Array#shift() method..
40 lines (29 loc) • 709 B
JavaScript
var log = console.log
, print = function ( ms, n ) {
var avg = 1000 * n / ms;
log( '- total elements: %d.', n );
log( '- elapsed: %d secs.', ms / 1000 );
log( '- average: %d Mel/sec.', ( avg / 1000 / 1000 ).toFixed( 2 ) );
}
, Train = require( '../' )
, t = new Train()
, p = 24
, k = Math.pow( 2, p )
, i = 0
, result = []
, stime = 0
, etime = 0
;
log( '- fill Train with 2^%d items', p );
i = k;
for ( ; i--; ) {
t.push( 1 );
};
log( '- evicting %d items with single #pop()', k );
t.qhead.push( 1 );
i = k;
stime = Date.now();
result = t.pop( k );
etime = Date.now() - stime;
print( etime, k );