Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Clamp01 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
92304448.0 Ops/sec
Using ternary
68817968.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 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);