question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

optimization: division by constant as multiplication by inverse

See original GitHub issue

Couldn’t division by constants be optimized by replacing the operation by a multiplication of its inverse (i.e x / 2 === x * (1/2) === x * 0.5)

input

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

function hello(x) {
 alert( x / 5);
}
const x = prompt('x')
hello(x);

output

alert(prompt("x")/5);

better optimization

alert(prompt("x")*0.2);

see example on “closure-compiler.appspot.com

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gerardolimacommented, Sep 5, 2019

hey, @tjgq, generally I’d agree with you, but there are more js engines around than I could benchmark (*). I expect closure-compiler project would have such test suits, though, that could shed some hard evidence on these numbers.

(*) some time ago, I did such benchmark (here) and, at the time on my computer, chrome yielded 14% better performance and IE 10%.

1reaction
tjgqcommented, Sep 5, 2019

Why do you believe this to be an optimization? For an integer divisor, multiplication by the reciprocal will always result in increased code size (in your example, *0.2 is longer than /5).

If the intent is to optimize for execution speed rather than code size, this would need to be justified by a benchmark. It’s very likely that JavaScript engines already perform the optimization you describe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Will the compiler optimize division into multiplication
For platforms with IEEE-754 arithmetic, this is true for constant divisors that are a power of 2, when the inverse is representable. I...
Read more >
How do compilers optimize divisions? - Hacker News
So: Multiply by modular inverse to get N, check that it's small enough that 3N < 2^32 ore equivalently that N < 2^32...
Read more >
How can I reverse optimized integer division/modulo by ...
One way is to simply realize that division is the same as multiplication with the inverse of the number, i.e . The problem...
Read more >
Faster remainders when the divisor is a constant
Suppose that you want to divide a variable n by a constant d. You have that n/d = n * (2 N /d)...
Read more >
Integer Division by Constants: Optimal Bounds
odd integer by multiplying by a fractional inverse, followed by some rounding. Artzy et al. [1] describe a related algorithm to divide multiples...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found