Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active June 6, 2021 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/36f7b2dcf6cc13f1c632b595d43db0c0 to your computer and use it in GitHub Desktop.
Save brainysmurf/36f7b2dcf6cc13f1c632b595d43db0c0 to your computer and use it in GitHub Desktop.
Performance of various iterations in AppsScripts

Performance

Screenshot of result of running main:

image

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});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment