Created
August 11, 2011 23:33
-
-
Save uhop/1141073 to your computer and use it in GitHub Desktop.
What's faster: ?:, Math.min(), or Math.max()?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What's faster: "if", ?:, Math.min(), or Math.max()? | |
this.group( | |
"Min/max comparison", | |
function baseline(){ | |
// this one does nothing, it is here only for comparison | |
var a = Math.random(), b = Math.random(), | |
c = a; | |
}, | |
function if_stmt(){ | |
var a = Math.random(), b = Math.random(), | |
c = a; | |
if(c < b){ c = b; } | |
}, | |
function ternary(){ | |
var a = Math.random(), b = Math.random(), | |
c = a < b ? a : b; | |
}, | |
function ternary_assignment(){ | |
var a = Math.random(), b = Math.random(), | |
c = a; | |
c = c < b ? c : b; | |
}, | |
function min(){ | |
var a = Math.random(), b = Math.random(), | |
c = Math.min(a, b); | |
}, | |
function max(){ | |
// this one does the opposite, but the speed should be the same as min() | |
var a = Math.random(), b = Math.random(), | |
c = Math.max(a, b); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark it with http://www.perfjs.com/