Multiple choice technology performance

Which for loop will run fastest?

  1. for (var i = 0; i < arr.length; i++) { // some code here }

  2. for (var i = 0, len = arr.length; i < len; i++) { // some code here }

  3. for (var i = arr.length; i--; ) { // some code here }

  4. for ( i = 0; i < arr.length; i++) { // some code here }

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

A decrementing loop checking against zero (i--) is historically the fastest in JavaScript because the termination check does not require querying a property (arr.length) or comparing variables on every iteration. Other options perform redundant length lookups or property evaluations inside the loop condition.