loading
open in
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Data definition
var primeNumbers = [2,3,5,7,11];

// Option 1) Oldest way & least efficient, assigns length on each iteration and i hoisted to the top
var loopStart = performance.now();
for (var i = 0; i < primeNumbers.length; i++) {
  console.log(primeNumbers[i]);
}
console.log( performance.now() -loopStart + ' ms')

// Option 2) Improved loop, assigns length to variable & defines i to avoid hoisting
var dataLength = primeNumbers.length, i;

var loopStart = performance.now();
for (i = 0; i < dataLength; i++) {
  console.log(primeNumbers[i]);
}
console.log(performance.now() - loopStart + ' ms')
Native Browser JavaScript