array peek (v5)

Revision 5 of this benchmark created by Viesturs on


Setup

var a = [];
    for (var i = 0; i < 100; i ++) a.push(i*2);
    
    function Peek1(a) {
       return a[a.length-1];
    }
    
    function Peek2(a) {
       var x = a.pop();
       a.push(x);
       return x;
    }
    
    function Peek3(a) {
       return a.slice(-1)[0];
    }
    
    function Peek4(a) {
       return a.slice(-1).pop();
    }

Test runner

Ready to run.

Testing in
TestOps/sec
peek direct
Peek1(a);
ready
peek pop push
Peek2(a);
ready
peek slice
Peek3(a);
ready
peek slice pop
Peek4(a);
ready

Revisions

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