]> git.wincent.com - hextrapolate.git/commitdiff
Avoid repeated indexOf calls
authorGreg Hurrell <greg@hurrell.net>
Wed, 31 May 2017 15:06:21 +0000 (08:06 -0700)
committerGreg Hurrell <greg@hurrell.net>
Wed, 31 May 2017 15:06:21 +0000 (08:06 -0700)
src/getDigits.js

index 64a1bc851bbe87fc216b909fa1ede0b333732575..0bdc7d16846c18c4ccd09ca8e55cf63c501bbd4b 100644 (file)
@@ -7,6 +7,11 @@
 
 import DIGITS from './DIGITS';
 
+const DIGITS_MAP = {};
+DIGITS.split('').forEach((digit, i) => {
+  DIGITS_MAP[digit] = i;
+});
+
 /**
  * Strips the leading prefix from `number` in `base` and returns the remaining
  * part of the string.
@@ -28,7 +33,8 @@ function stripPrefix(number: string, base: number): string {
 
 function parse(digit: string, base: number) {
   if (base > 36 && base <= 62) {
-    const position = DIGITS.indexOf(digit);
+    // This branch only called for serialization to/from the URL.
+    const position = DIGITS_MAP[digit];
     return position !== -1 && position < base ? position + 1 : NaN;
   } else {
     return parseInt(digit, base);