Created
October 7, 2019 14:19
-
-
Save uhyo/a33eb07bd19a1344076bd9574b5e6385 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 numBegArr = ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; | |
const numBegSet = new Set(numBegArr); | |
const isNumericBeginning = char => numBegSet.has(char); | |
const isNumericBeginning2 = char => numBegArr.includes(char); | |
const isNumericBeginning3 = char => | |
char === "-" || | |
char === "0" || | |
char === "1" || | |
char === "2" || | |
char === "3" || | |
char === "4" || | |
char === "5" || | |
char === "6" || | |
char === "7" || | |
char === "8" || | |
char === "9"; | |
const N = 1e6; | |
console.time("isNumericBeginning"); | |
for (let i = 0; i < N; i++) { | |
for (const char of "-0123456789") { | |
isNumericBeginning(char); | |
} | |
} | |
console.timeEnd("isNumericBeginning"); | |
console.time("isNumericBeginning2"); | |
for (let i = 0; i < N; i++) { | |
for (const char of "-0123456789") { | |
isNumericBeginning2(char); | |
} | |
} | |
console.timeEnd("isNumericBeginning2"); | |
console.time("isNumericBeginning3"); | |
for (let i = 0; i < N; i++) { | |
for (const char of "-0123456789") { | |
isNumericBeginning3(char); | |
} | |
} | |
console.timeEnd("isNumericBeginning3"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment