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
Manual Math.min Math.max
144.1M/s
Conditional Math clamp
123.5M/s
Lodash clamp
50.1M/s
View exact numbers
Test name
Executions per second
✓
Manual Math.min Math.max
144,070,064 Ops/sec
Conditional Math clamp
123,499,744 Ops/sec
Lodash clamp
50,137,584 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; })();