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
Go 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:
2 days ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:153.0) Gecko/153.0 Firefox/153.0
Browser/OS:
Firefox Mobile 153 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Using ternary
38.9M/s
Using if
38.8M/s
View exact numbers
Test name
Executions per second
✓
Using ternary
38,910,032 Ops/sec
Using if
38,831,368 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?