Created
December 22, 2022 17:45
-
-
Save caderek/4073ce60494839384c3fa1cdc1c8e215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NUMBER_OF_FORMULAS = 1000; | |
const generateName = (num) => { | |
const charCode = (num % (122 - 96)) + 97; | |
const count = Math.floor(num / 26); | |
return String.fromCharCode(charCode).repeat(count + 1); | |
}; | |
const formulas = new Map( | |
Array.from({ length: NUMBER_OF_FORMULAS }, (_, i) => [ | |
generateName(i), | |
(x, y) => x + y + i, | |
]) | |
); | |
function calculateWithMap(formula, x, y) { | |
let calcResult = 0; | |
if (formula === "average") { | |
for (const [_, formula] of formulas) { | |
calcResult += formula(x, y); | |
} | |
calcResult /= formulas.size; | |
} else { | |
const calc = formulas.get(formula); | |
if (calc) { | |
calcResult = calc(x, y); | |
} | |
} | |
return Math.round(calcResult); | |
} | |
export default calculateWithMap; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment