push/pop vs unshift/shift vs submit/receive (v25)

Revision 25 of this benchmark created by Meneer R on


Preparation HTML

<script>

    function Iterator( c ){
        this.buffer = [];
        this.next = [];
        this.skip = 0;
        this.cutoff = c;
    }
    Iterator.prototype.submit = function( obj ){
        if( obj === undefined ) return;
        if( this.skip > this.cutoff ) this.next.push( obj );  
        else this.buffer.push( obj );
    }
    Iterator.prototype.receive = function(){
        if( this.buffer.length === 0 ) return undefined;
        if( this.skip < this.buffer.length ){
            var result = this.buffer[this.skip];
            this.skip = this.skip + 1;
            return result;
        }       
        this.buffer = this.next;
        this.next = [];
        this.skip = 1;        
        return this.buffer[0];
    }

var cx = [];
for( var i=5; i<15; i++ ){
  cx.push({
    s: (Math.random() * i) + (Math.random() * i * i * i * i), 
    r: (Math.random() * i * 10) + (Math.random() * i * i * i) 
  });  
}
console.log( cx );
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Push/shift
var ps = [];
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.push( i*j );
  for( var i=0;i<t2; i++ ) ps.shift();  
}
ready
Unshift/pop
var ps = [];
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.unshift( i*j );
  for( var i=0;i<t2; i++ ) ps.pop();  
}
ready
push/pop
var ps = [];
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.push( i*j );
  for( var i=0;i<t2; i++ ) ps.pop();  
}
ready
submit/receive(1000)
var ps = new Iterator( 1000 );
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.submit( i*j );
  for( var i=0;i<t2; i++ ) ps.receive();  
}
ready
submit/receive(10)
var ps = new Iterator( 10 );
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.submit( i*j );
  for( var i=0;i<t2; i++ ) ps.receive();  
}
ready
submit/receive(-1)
var ps = new Iterator( -1 );
for( var j=0; j<10; j=j+2 ){
  var t1 = cx[j]; var t2 = cx[j+1];
  for( var i=0;i<t1; i++ ) ps.submit( i*j );
  for( var i=0;i<t2; i++ ) ps.receive();  
}
ready

Revisions

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