Multiply vs divide

JavaScript performance comparison

Test case created

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Multiply
var x = 5 * 0.25,
    y = 5 * 0.5,
    z = 5 * 0.1,
    a = 5 * 2;
pending…
Divide
var x = 5 / 4,
    y = 5 / 2,
    z = 5 / 10,
    a = 5 / 0.5;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Miller Medeiros commented :

Browsers are smart, behavior might change based on the condition, see this perf test: http://jsperf.com/multiply-vs-divide/3

And read more about what might be going on this test: http://blog.millermedeiros.com/benchmarking-is-hard/ - TL;DR; JIT and caching are giving misleading results in this case.

I'm not saying you should always use division, I've seen different behavior based on browser and usage. This is probably a "premature optimization" unless you need to do something over an insane amount of data (eg. processing pixels on a large image).

Always profile your code and make sure it does apply to your use case. Also make sure your perf tests aren't biased.

Cheers.

Add a comment