Skip to content

Instantly share code, notes, and snippets.

@jalal246
Last active October 14, 2019 14:36
Show Gist options
  • Select an option

  • Save jalal246/a8e8f38416acb72fdff9a6be83e5ea9a to your computer and use it in GitHub Desktop.

Select an option

Save jalal246/a8e8f38416acb72fdff9a6be83e5ea9a to your computer and use it in GitHub Desktop.

Motivation

Which is best way to execute scope of code inside loop? Splitting it into functions. Calling nested functions as inner functions or calling function outside loop?

Conclusion

  • test without functions took: 158.88999996241182 ms
  • test with calling functions inside took: 170.85500003304332 ms
  • test with calling functions outside took: 165.08999996585771 ms
  • test with calling functions inside depending on global parm took: 310.6550000375137 ms

Seems like all results nearly identical except passing params globally., with slightly enhancement in executing loop without functions.

/**
* Utility, generates random number.
*/
function randNumber() {
return Math.ceil(Math.random() * 10);
}
/**
* Utility, generates random number between 1 and 3.
*/
function diceWithMaxThee() {
return Math.floor(Math.random() * 3) + 1;
}
function $isUp(x1, x2, y1, y2) {
return x1 > x2 && y1 > y2;
}
function $isDown(x1, x2, y1, y2) {
return x1 < x2 && y1 < y2;
}
function $isZero(x1, x2, y1, y2) {
return x1 < x2 || y1 < y2;
}
function testCallFuncsOutside({ isUp, isDown, x1, x2, y1, y2 }) {
let isQualified;
if (isUp) {
isQualified = $isUp(x1, x2, y1, y2);
} else if (isDown) {
isQualified = $isDown(x1, x2, y1, y2);
} else {
isQualified = $isZero(x1, x2, y1, y2);
}
return isQualified;
}
function testCallFuncsInside({ isUp, isDown, x1, x2, y1, y2 }) {
function _isUp(x1, x2, y1, y2) {
return x1 > x2 && y1 > y2;
}
function _isDown(x1, x2, y1, y2) {
return x1 < x2 && y1 < y2;
}
function _isZero(x1, x2, y1, y2) {
return x1 < x2 || y1 < y2;
}
let isQualified;
if (isUp) {
isQualified = _isUp(x1, x2, y1, y2);
} else if (isDown) {
isQualified = _isDown(x1, x2, y1, y2);
} else {
isQualified = _isZero(x1, x2, y1, y2);
}
return isQualified;
}
function testWithoutFuncs({ isUp, isDown, x1, x2, y1, y2 }) {
let isQualified;
if (isUp) {
isQualified = x1 > x2 && y1 > y2;
} else if (isDown) {
isQualified = x1 < x2 && y1 < y2;
} else {
isQualified = x1 < x2 || y1 < y2;
}
return isQualified;
}
let isUp;
let isDown;
let x1;
let x2;
let y1;
let y2;
function testCallFuncsInsideWithGlobalParam() {
function _isUp(x1, x2, y1, y2) {
return x1 > x2 && y1 > y2;
}
function _isDown(x1, x2, y1, y2) {
return x1 < x2 && y1 < y2;
}
function _isZero(x1, x2, y1, y2) {
return x1 < x2 || y1 < y2;
}
let isQualified;
if (isUp) {
isQualified = _isUp(x1, x2, y1, y2);
} else if (isDown) {
isQualified = _isDown(x1, x2, y1, y2);
} else {
isQualified = _isZero(x1, x2, y1, y2);
}
return isQualified;
}
const MAX_LOOP = 10000000;
function runTest() {
const generatedParams = [];
for (let i = 0; i < MAX_LOOP; i++) {
const x1 = randNumber();
const x2 = randNumber();
const y1 = randNumber();
const y2 = randNumber();
const result = diceWithMaxThee();
let isUp = false;
let isDown = false;
if (result === 1) {
isUp = true;
} else if (result === 2) {
isDown = true;
}
generatedParams.push({ isUp, isDown, x1, x2, y1, y2 });
}
/**
*
*/
const perf1 = performance.now();
for (let i = 0; i < MAX_LOOP; i++) {
testWithoutFuncs(generatedParams[i]);
}
console.log(`test without functions took: ${performance.now() - perf1} ms`);
const perf2 = performance.now();
for (let i = 0; i < MAX_LOOP; i++) {
testCallFuncsInside(generatedParams[i]);
}
console.log(
`test with calling functions inside took: ${performance.now() - perf2} ms`
);
const perf3 = performance.now();
for (let i = 0; i < MAX_LOOP; i++) {
testCallFuncsOutside(generatedParams[i]);
}
console.log(
`test with calling functions outside took: ${performance.now() - perf3} ms`
);
const perf4 = performance.now();
for (let i = 0; i < MAX_LOOP; i++) {
({ isUp, isDown, x1, x2, y1, y2 } = generatedParams[i]);
testCallFuncsInsideWithGlobalParam();
}
console.log(
`test with calling functions inside depending on global parm took: ${performance.now() -
perf4} ms`
);
generatedParams.length = 0;
}
runTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment