Math.min vs ternary vs if

JavaScript performance comparison

Revision 7 of this test case created

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 = 9,
      c = Math.random(),
      d = Math.random()
    ;
};
</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
static min (Math)
a = Math.min(a, b);
pending…
static min (if)
if (b < a) {
  a = b;
}
pending…
static min (ternary)
a = a < b ? a : b;
pending…
min (Math)
c = Math.min(c, d)
pending…
min (if)
if (d < c) {
  c = d;
}
pending…
min (ternary)
c = c < d ? c : d;
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:

0 comments

Add a comment