loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Data definition
var primeNumbers = [2,3,5,7,11];
var jsFramework = {'name':'React','creator':'Facebook','purpose':'UIs'}

// Option 1 ES5 forEach on array
var loopStartOpt1 = performance.now();
primeNumbers.forEach(function (value) {
  console.log(value);
});
console.log(performance.now() - loopStartOpt1 + ' ms')

// Option 2 ES5 forEach on Object
var loopStartOpt2 = performance.now();
Object.keys(jsFramework).forEach(function(property){
    console.log(property+' : '+jsFramework[property]);
});
console.log(performance.now() - loopStartOpt2 + ' ms')


// Test functions and data for other ES5 iteration methods
function isPrime(element, index, array) { 
  return element / 1; 
} 
function evenNumber(element,index,array) { 
  if (element % 2 === 0)
    return element;
}
function allCaps(str) {
    return str.toUpperCase()
}
var pageHits = ['home.html', 'react.html', 'home.html', 'angular.html', 'home.html','nodejs.html','react.html'];

function countPageHits(allPages, page) { 
  if (page in allPages) {
    allPages[page]++;
  }
  else {
    allPages[page] = 1;
  }
  return allPages;
}

//Other ES5 iteration method samples
console.log(primeNumbers.every(isPrime));//True
console.log(primeNumbers.every(evenNumber));//False
console.log(primeNumbers.filter(evenNumber));//[2]
console.log(primeNumbers.some(evenNumber));//True
console.log(Object.keys(jsFramework).map(allCaps));//[ 'NAME', 'CREATOR', 'PURPOSE' ]
console.log(pageHits.reduce(countPageHits,{}));
console.log(pageHits.reduceRight(countPageHits,{}));
Native Browser JavaScript