Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clamp01 with various implementations
(version: 1)
Comparing performance of:
Using if vs Using ternary
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
Using if
const clamp01 = (x) => { if (x <= 0) return 0; if (x >= 1) return 1; return x; } clamp01(0.5); clamp01(1e-30); clamp01(-1); clamp01(1.5); clamp01(0); clamp01(1); clamp01(Infinity); clamp01(-Infinity); clamp01(NaN);
Using ternary
const clamp01 = (x) => { return (x < 0) ? 0 : ((x > 1) ? 1 : x); } clamp01(0.5); clamp01(1e-30); clamp01(-1); clamp01(1.5); clamp01(0); clamp01(1); clamp01(Infinity); clamp01(-Infinity); clamp01(NaN);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using if
Using ternary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using if
92304448.0 Ops/sec
Using ternary
68817968.0 Ops/sec
Related benchmarks:
Multiplication vs Math.exp
Nullish vs If
Parse number to bigint
Joining strings 2
Joining strings 3
Joining strings 4
test early return
Test direct and destructuring performances
Clamp with various implementations
Comments
Confirm delete:
Do you really want to delete benchmark?