|
/** |
|
* 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(); |