Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Safari 15
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
Clamp from 0 to 255 using ternary 465750.0 Ops/sec
Clamp from 0 to 255 using Min/Max 294430.6 Ops/sec
Clamp from 0 to 255 with IF 1359837.2 Ops/sec
Clamp from 0 to 255 with unary && 1379515.5 Ops/sec
Clamp in arrow function with ternary 854807.3 Ops/sec
Clamp in regular function with MinMax 396250.8 Ops/sec
Clamp in arrow function with IF 880335.2 Ops/sec
Clamp in regular function with unary 891079.6 Ops/sec
Script Preparation code:
Tests:
  • Clamp from 0 to 255 using ternary

  • Clamp from 0 to 255 using Min/Max

  • Clamp from 0 to 255 with IF

  • Clamp from 0 to 255 with unary &&

  • Clamp in arrow function with ternary

  • Clamp in regular function with MinMax

  • Clamp in arrow function with IF

  • Clamp in regular function with unary