array peek (v2)

Revision 2 of this benchmark created on


Setup

var a = [1, 2, 3, 4];
    
    function Peek1(a) {
       if (a.length>0) return a[a.length-1];
    }
    
    function Peek2(a) {
       var n = a.length;
       if (n>0) return a[n-1];
    }
    
    function Peek3(a) {
       var n;
       if (n=a.length) return a[n-1];
    }
    
    function Peek4(a){
    var x;a.push(x=a.pop());return x;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
peek direct
Peek1(a);
ready
peek cache length
Peek2(a);
ready
ugly
Peek3(a);
ready
Pop Push
Peek4(a)
ready

Revisions

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