Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Clamp with various implementations
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
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:
Chrome 134
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Using if
96048936.0 Ops/sec
Using min and max
89857664.0 Ops/sec
Using ternary
99262288.0 Ops/sec
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);