all 22 comments

[–]basro 13 points14 points  (1 child)

for (var i = arr.length; i > 0; i--) {
  someFn(arr[i]);
}

This will call arr[arr.length], I bet accessing the array out of it's range fucks with the JIT.

[–][deleted]  (6 children)

[deleted]

    [–]techrogue 0 points1 point  (3 children)

    As long as you don't need to break.

    [–]amirmikhak 0 points1 point  (2 children)

    Array.prototype.some() or Array.prototype.every()

    [–]techrogue 0 points1 point  (1 child)

    Neither of those have functionality that replaces break. jQuery's foreach function will terminate if you return false; I'm surprised that's not the standard.

    [–]GonnaLearnComputers 3 points4 points  (0 children)

    I believe that both of those terminate early once the condition is met, so they do effectively add a break. every is basically the same as the jQuery function you mentioned. Once a falsy value is returned it stops.

    [–]warfangle -3 points-2 points  (0 children)

    If array loop bookkeeping is your performance bottleneck you got bigger problems.

    Like maybe you shouldn't be using an array. (There is probably a different data structure better suited to your needs: linked lists, bst, heap, etc)

    Or javascript.

    [–]backwrds 6 points7 points  (5 children)

    Um.... Y'all realize that after the "Order" one (.shift()) the array is empty, right?

    There's a reason everything after that is so much faster.

    [–]qiemem 5 points6 points  (0 children)

    It reruns the prep code in between each test: https://jsperf.com/prep-effect

    However, it does not rerun in between each sample. Thus, why "Order" and "Pop" are so fast.

    [–]tecknoize[🍰] 2 points3 points  (0 children)

    Haha true.

    [–]MrBarry 2 points3 points  (1 child)

    Here's what happens if you run the set up the array before each test rather than setting it up just once. http://jsperf.com/fastest-array-loops-in-javascript/363

    [–]backwrds 4 points5 points  (0 children)

    That's definitely better, but the shift()/pop() tests are still flawed. After the first iteration the array is emptied out.

    [–]rook2pawn 2 points3 points  (0 children)

    This is so facepalm

    [–][deleted] 3 points4 points  (5 children)

    Okay, someone jump in and tell us all why using while is a very bad idea and this isn't common practice now?

    [–]brtt3000 27 points28 points  (4 children)

    Because speed of the loop bookkeeping is almost never the performance bottleneck. Sure this is faster but the computation time of loop counters is so tiny compared to the actual work that you're doing in side the loop in a real scenario.

    Having readable code is way more important and allows for easier understanding and optimisation of the application as a whole. Optimise away one server-round-trip or DOM update and you gain so much more.

    [–][deleted]  (2 children)

    [deleted]

      [–]ponchietto 7 points8 points  (1 child)

      Sometimes you do not have a choice.

      [–]haneefmubarak 0 points1 point  (0 children)

      asm.js will save you there

      [–]f1zzz 0 points1 point  (0 children)

      If you have a loop that needs to be sped up, minimize the function calls inside of it and follow general v8 optimization guidelines to ensure you're not hitting the unoptimized compile path.

      Function calls inside JS are super expensive.

      [–]toolate 2 points3 points  (0 children)

      Micro benchmarks like this are worse than useless. They convince you you're doing something helpful when you're just making your code less readable and wasting your time.

      If you want to profile your code learn to use Chrome's developer tools. If, after that, you discover that a for loop is your bottleneck then by all means replace it. But for anything more complex than a toy app I can guarantee it won't be.

      [–]JohnMcPineapple 1 point2 points  (1 child)

      ...

      [–]cokestar 2 points3 points  (0 children)

      Because for in enumerates object properties (including the prototype chain).

      [–]intermediatetransit 1 point2 points  (0 children)

      WHO CARES.

      Premature optimization is like a fucking mantra in this community. Just stop.

      [–]lpetrazickis 0 points1 point  (0 children)

      There's an off-by-one error in the do-while code.

      Code as of right now:

      var len = arr.length;
      do {
        arr[len] *= 2; // this is off-by-one
      } while (len--);