As predicted, this is faster. Not sure if as fast as what I had before.
Will do actual profiling and revert to prior state if it ends up being
better.
const aDigit = i < aLength ? aDigits[aLength - i - 1] : 0;
const bDigit = i < bLength ? bDigits[bLength - i - 1] : 0;
const sum = aDigit + bDigit + carry;
const aDigit = i < aLength ? aDigits[aLength - i - 1] : 0;
const bDigit = i < bLength ? bDigits[bLength - i - 1] : 0;
const sum = aDigit + bDigit + carry;
- result.unshift(sum % base);
+ result.push(sum % base);
// ~~ here is the equivalent of Math.floor; used to avoid V8 de-opt,
// "Reference to a variable which requires dynamic lookup".
carry = ~~(sum / base);
}
// ~~ here is the equivalent of Math.floor; used to avoid V8 de-opt,
// "Reference to a variable which requires dynamic lookup".
carry = ~~(sum / base);
}
- return result.length ? result : [0];
+ return result.length ? result.reverse() : [0];