Which is the fastest way to loop? [for curiosity]

If I am reading your question correctly, you want to time the fastest way to preform some kind of loop.

There are a number of looping constructions in JavaScript that include:

  • for
  • while
  • do... while
  • recursion
  • setTimeout/setInterval
  • for...in
  • for...of
  • generators

Of these, the basic for loop is generally the fastest as it has the least overhead of any of the others.

setTimeout and setInterval will be among the slower because the 0 delay is not honored. Browsers do set up some internal minimum delay.

I’ve never used it, but I believe there is postMessage that can be used without delay.

Leave a Comment