array peek (v4)

Revision 4 of this benchmark created on


Setup

var a = [1, 2, 3, 4, 1, 2, 3, 4];
    
    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.