Screenshot of result of running main
:
Info forEachIteration_ took 9796 milliseconds
Info forOfLetIteration_ took 12521 milliseconds
Info forOfConstIteration_ took 12309 milliseconds
function measure_({func, arr}) { | |
const start = new Date().getTime(); | |
func.call(null, arr); | |
const end = new Date().getTime(); | |
const delta = end - start; | |
const report = `${func.name} took ${delta} milliseconds`; | |
Logger.log(report); | |
return delta; | |
} | |
function add100_(v) { | |
return v + 10 * v; | |
} | |
function perform_(value) { | |
for (let x = value; x >= 0; x--) { | |
// just do some math that is complex enough to take some time | |
const y = x * 100 / 3293.332 - (32); | |
const b = 100 - add100_(y); | |
} | |
} | |
function forOfConstIteration_ (arr) { | |
for (const value of arr) { | |
perform_(value); | |
} | |
} | |
function forOfLetIteration_ (arr) { | |
for (let value of arr) { | |
perform_(value); | |
} | |
} | |
function forEachIteration_ (arr) { | |
arr.forEach(perform_); | |
} | |
function main () { | |
const length = 100000; | |
const arr = [...Array.from({length}).keys()]; | |
const funcs = [forEachIteration_, forOfLetIteration_, forOfConstIteration_]; | |
for (const func of funcs) { | |
measure_({func, arr}); | |
} | |
} |