The commit message "Optimized performance of the GCD algorithm" piqued my interest. It's GCD, what's there to optimize? I noticed the following code: while (b !== 0) { if (a > b) { a -= b; } else { b -= a; } } This uses repeated subtraction (a -= b) to implement division with remainder (a %= b); imagine a=1e12+1, b=2. Please don't do this in your code. If there is a built it instruction for division with remainder, d…
This is the Euclidean algorithm for GCD, it's naive and simple. But if you see it more carefully, the commit that says "Optimized performance ..." contains just the lines: 34 var tmp = a; 35 a = Math.max(a, b); 36 b = Math.min(tmp, b); 37 if (a % b === 0) return b; to avoid the unnecessary repetitions if b is already the GCD
I'm surprised the author didn't implement the binary gcd, however, although it has the same big-O as Euclid: https://en.wikipedia.org/wiki/Binary_GCD_algorithm