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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser:
Chrome 147
Operating system:
Windows
Device Platform:
Desktop
Date tested:
3 months ago
Manual Math.min Math.max
169.9M/s
Conditional Math clamp
166.8M/s
Lodash clamp
58.6M/s
View exact numbers
Test name
Executions per second
✓
Manual Math.min Math.max
169,907,488 Ops/sec
Conditional Math clamp
166,829,616 Ops/sec
Lodash clamp
58,578,200 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; })();