Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Chrome 125
Linux
Desktop
2 years ago
Benchmark.js
Clamp from 0 to 255 with unary &&
771K/s
Clamp from 0 to 255 with IF
765K/s
Clamp in arrow function with ternary
455K/s
Clamp in arrow function with IF
440K/s
Clamp in regular function with unary
417K/s
Clamp from 0 to 255 using ternary
353K/s
Clamp from 0 to 255 using Min/Max
236K/s
Clamp in regular function with MinMax
199K/s
View exact numbers
Test name Executions per second
Clamp from 0 to 255 with unary && 770,531 Ops/sec
Clamp from 0 to 255 with IF 764,645 Ops/sec
Clamp in arrow function with ternary 454,706 Ops/sec
Clamp in arrow function with IF 440,478 Ops/sec
Clamp in regular function with unary 417,289 Ops/sec
Clamp from 0 to 255 using ternary 353,035 Ops/sec
Clamp from 0 to 255 using Min/Max 236,481 Ops/sec
Clamp in regular function with MinMax 198,991 Ops/sec
Script Preparation code:
Tests: