Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
JavaScript: clamp() variants
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/123.0.0.0 Safari/537.36
Browser:
Chrome 123
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Benchmark engine:
Benchmark.js
With ternary operator #2
182.7M/s
With if statements
177.4M/s
With ternary operator #1
171.2M/s
With inline statements
168.6M/s
With min() and max()
8.7M/s
View exact numbers
Test name
Executions per second
✓
With ternary operator #2
182,734,624 Ops/sec
With if statements
177,379,744 Ops/sec
With ternary operator #1
171,189,504 Ops/sec
With inline statements
168,627,248 Ops/sec
With min() and max()
8,712,377 Ops/sec
Tests:
With inline statements
function clamp(n) { n < 0 && (n = 0); n > 100 && (n = 100); return n; } const n = clamp(150);
With min() and max()
function clamp(n) { return Math.min(Math.max(n, 0), 100); } const n = clamp(150);
With if statements
function clamp(n) { if (n < 0) n = 0; if (n > 100) n = 100; return n; } const n = clamp(150);
With ternary operator #1
function clamp(n) { n < 0 ? n = 0 : n; n > 100 ? n = 100 : n; return n; } const n = clamp(150);
With ternary operator #2
function clamp(n) { return n = n > 100 ? 100 : n < 0 ? 0 : n; } const n = clamp(150);