Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Lodash clamp VS Math.min, Math.max (Conditional)
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36
Browser:
Chrome Mobile 135
Operating system:
Android
Device Platform:
Mobile
Date tested:
one year ago
Test name
Executions per second
Lodash clamp
16092474.0 Ops/sec
Manual Math.min Math.max
30114890.0 Ops/sec
Conditional Math clamp
29840674.0 Ops/sec
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.7.11/lodash.min.js'></script>
Script Preparation code:
const clamp = (number, boundOne, boundTwo) => { if (!boundTwo) { return Math.max(number, boundOne) === boundOne ? number : boundOne; } else if (Math.min(number, boundOne) === number) { return boundOne; } else if (Math.max(number, boundTwo) === number) { return boundTwo; } return number; };
Tests:
Lodash clamp
_.clamp(150, 10, 100);
Manual Math.min Math.max
Math.min(Math.max(150, 10), 100);
Conditional Math clamp
((number = 150, boundOne = 10, boundTwo = 100) => { if (!boundTwo) { return Math.max(number, boundOne) === boundOne ? number : boundOne; } else if (Math.min(number, boundOne) === number) { return boundOne; } else if (Math.max(number, boundTwo) === number) { return boundTwo; } return number; })();