Math.min/max vs ternary vs if

JavaScript performance comparison

Test case created by Bemi Faison

Info

What's the fastest way to compare numbers? All conditionals are designed to fail.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var
      a = 10,
      b = 100,
      c
    ;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
min (Math)
c = Math.min(a, b);
pending…
max (Math)
c = Math.max(a, b);
pending…
min (if)
if (b < a) {
  c = a;
} else {
  c = b;
}
 
pending…
max (if)
if (a < b) {
  c = a;
} else {
  c = b;
}
pending…
min (ternary)
c = a > b ? a : b;
pending…
max (ternary)
c = a < b ? a : b;
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

tomByrer commented :

The FireFox Beta 16a1 & 12.2.1 are both tested on the same Intel i7 1st gen laptop. PaleMoon 12.2.1 = FireFox x64 compiled for "modern" CPUs. I get the test result "Infinity" for Min(math); that is why it is blank.

Add a comment