Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clamp with various implementations
(version: 1)
Comparing performance of:
Using if vs Using min and max 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 clamp = (x, minValue, maxValue) => { if (minValue > maxValue) { const temp = minValue; minValue = maxValue; maxValue = temp; } if (x <= minValue) return minValue; if (x >= maxValue) return maxValue; return x; } clamp(5, 0, 10); clamp(-10, 0, 10); clamp(15, 0, 10); clamp(0, 0, 10); clamp(10, 0, 10); clamp(5, 5, 5); clamp(10, 5, 5); clamp(-3, 5, 5); clamp(-5, -10, 0); clamp(-15, -10, 0); clamp(5, -10, 0); clamp(5, 10, 0); clamp(-5, 10, 0); clamp(15, 10, 0); clamp(100, 0, Infinity); clamp(-100, -Infinity, 100); clamp(NaN, 0, 10);
Using min and max
const clamp = (x, minValue, maxValue) => Math.max(minValue, Math.min(x, maxValue)); clamp(5, 0, 10); clamp(-10, 0, 10); clamp(15, 0, 10); clamp(0, 0, 10); clamp(10, 0, 10); clamp(5, 5, 5); clamp(10, 5, 5); clamp(-3, 5, 5); clamp(-5, -10, 0); clamp(-15, -10, 0); clamp(5, -10, 0); clamp(5, 10, 0); clamp(-5, 10, 0); clamp(15, 10, 0); clamp(100, 0, Infinity); clamp(-100, -Infinity, 100); clamp(NaN, 0, 10);
Using ternary
const clamp = (x, minValue, maxValue) => { const lower = minValue < maxValue ? minValue : maxValue; const upper = minValue < maxValue ? maxValue : minValue; return x < lower ? lower : x > upper ? upper : x; } clamp(5, 0, 10); clamp(-10, 0, 10); clamp(15, 0, 10); clamp(0, 0, 10); clamp(10, 0, 10); clamp(5, 5, 5); clamp(10, 5, 5); clamp(-3, 5, 5); clamp(-5, -10, 0); clamp(-15, -10, 0); clamp(5, -10, 0); clamp(5, 10, 0); clamp(-5, 10, 0); clamp(15, 10, 0); clamp(100, 0, Infinity); clamp(-100, -Infinity, 100); clamp(NaN, 0, 10);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using if
Using min and max
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
97812976.0 Ops/sec
Using min and max
96997168.0 Ops/sec
Using ternary
99219888.0 Ops/sec
Related benchmarks:
Assigning new variable
Test array concat
Joining strings 2
Joining strings 3
Joining strings 4
test early return
For vs Foreach manu
Test direct and destructuring performances
Clamp01 with various implementations
Comments
Confirm delete:
Do you really want to delete benchmark?