array peek (v6)

Revision 6 of this benchmark created by Doug Ludlow 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) {
       return a.slice(-1)[0];
    }

Test runner

Ready to run.

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

Revisions

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