max vs if < 0

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var x = 10
    var y = -10
    var v, w
};
</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
max
v = Math.max(x, 0)
w = Math.max(y, 0)
pending…
if
if (x < 0) {
  v = 0
} else {
  v = x
}

if (y < 0) {
  w = 0
} else {
  w = y
}
pending…
ternary
v = x < 0 ? 0 : x
w = y < 0 ? 0 : y
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