]> git.wincent.com - hextrapolate.git/commitdiff
Microoptimization
authorGreg Hurrell <greg@hurrell.net>
Fri, 2 Jun 2017 15:37:20 +0000 (08:37 -0700)
committerGreg Hurrell <greg@hurrell.net>
Fri, 2 Jun 2017 15:37:20 +0000 (08:37 -0700)
Which may or may not be perceptible, and I can't pick it up in the
profiler because it is too noisy.

src/addDigits.js

index 70b713c2a75d2be037a5dac2a45323cd0eaa66da..a2e9bc36b9fabbe090ac7bf0474edb854bffc5de 100644 (file)
@@ -16,9 +16,15 @@ export default function addDigits(
 ): Array<number> {
   let result = [];
   let carry = 0;
-  for (let i = 0; i < aDigits.length || i < bDigits.length || carry; i++) {
-    const aDigit = i < aDigits.length ? aDigits[i] : 0;
-    const bDigit = i < bDigits.length ? bDigits[i] : 0;
+  let aLength = aDigits.length;
+  let bLength = bDigits.length;
+  for (
+    let i = 0, max = Math.max(aLength, bLength);
+    carry || i < max;
+    i++
+  ) {
+    const aDigit = i < aLength ? aDigits[i] : 0;
+    const bDigit = i < bLength ? bDigits[i] : 0;
     const sum = aDigit + bDigit + carry;
     result.push(sum % base);