Math.min/max vs ternary vs if
JavaScript performance comparison
Info
What's the fastest way to compare numbers? All conditionals are designed to fail.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var
a = Math.random(),
b = Math.random(),
c = Math.random(),
d = Math.random()
;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test  Ops/sec  

min (Math) 

pending… 
max (Math) 

pending… 
min (if) 

pending… 
max (if) 

pending… 
min (ternary) 

pending… 
max (ternary) 

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:
 Revision 1: published by Bemi Faison
 Revision 2: published
 Revision 3: published
 Revision 7: published
 Revision 8: published
 Revision 9: published
 Revision 10: published by David
 Revision 11: published by David
 Revision 12: published by David
 Revision 14: published
 Revision 15: published by Tomasz
 Revision 16: published by Tomasz
 Revision 17: published by Joshua Wise
 Revision 18: published by mientjan
 Revision 19: published
 Revision 20: published
 Revision 21: published by elennaro
0 comments