Binary Exponentiation Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Binary Exponentiation runs in O(log n) time and O(1) space, in JavaScript as in every other language.
- ✓The ** operator and Math.pow operate on IEEE-754 doubles, which only represent integers exactly up to 2**53 -- useless for the exponent sizes (1e9, 1e18) binary exponentiation normally deals with.
- ✓Write the squaring loop directly over BigInt, applying % mod after every multiply, not just at the end -- exactly the shape you'd otherwise get for free from Python's or Java's builtin.
- ✓The JavaScript trap: base ** exp % mod on regular numbers silently produces a wrong answer long before it throws: once base ** exp exceeds 2**53 the double has already lost precision, so the modulus gets applied to a number that was never exact.
- ✓The BigInt squaring loop does the same roughly log2(exp) squarings as any other language, and each squaring is a V8 BigInt multiply, which scales better than schoolbook past a few hundred bits but still carries real per-call overhead for small operands -- BigInt is consistently slower than Number arithmetic for anything that would fit in 53 bits, so it should not be reached for below that range.
What Binary Exponentiation actually is
Binary Exponentiation in JavaScript: what you actually reach for
JavaScript implementation
function binPow(base, exp, mod) {
base = BigInt(base) % BigInt(mod);
exp = BigInt(exp);
mod = BigInt(mod);
let result = 1n;
while (exp > 0n) {
if (exp & 1n) result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1n;
}
return result;
}
// Number ** silently loses precision past 2**53 - no error, no NaN
const wrong = (3 ** 61) % 1_000_000_007; // already imprecise
const right = binPow(3, 61, 1_000_000_007); // exact, via BigIntThink Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Binary Exponentiation?
The ** operator and Math.pow operate on IEEE-754 doubles, which only represent integers exactly up to 2**53 -- useless for the exponent sizes (1e9, 1e18) binary exponentiation normally deals with. There is no modPow anywhere in the language; BigInt gives exact arbitrary-precision integers and its own ** operator, but BigInt has no modular-exponentiation builtin either, so the squaring loop has to be hand-written.
What is the most common JavaScript mistake when implementing Binary Exponentiation?
base ** exp % mod on regular numbers silently produces a wrong answer long before it throws: once base ** exp exceeds 2**53 the double has already lost precision, so the modulus gets applied to a number that was never exact. There is no error, no NaN, just a plausible-looking wrong result -- the only safe fix is switching every operand (base, exp, mod, and the accumulator) to BigInt before the first multiply.
How does Binary Exponentiation actually perform in JavaScript?
The BigInt squaring loop does the same roughly log2(exp) squarings as any other language, and each squaring is a V8 BigInt multiply, which scales better than schoolbook past a few hundred bits but still carries real per-call overhead for small operands -- BigInt is consistently slower than Number arithmetic for anything that would fit in 53 bits, so it should not be reached for below that range. Modding after every squaring keeps operands from ever exceeding twice the modulus's bit width, which is what keeps each multiply cheap. Time complexity is O(log n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying full price for a tool, check the student deals on AI and developer tools listed on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.