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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Lodash clamp
50137584.0 Ops/sec
Manual Math.min Math.max
144070064.0 Ops/sec
Conditional Math clamp
123499744.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; })();