Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Chrome 117
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
Clamp from 0 to 255 using ternary 891916.2 Ops/sec
Clamp from 0 to 255 using Min/Max 585927.0 Ops/sec
Clamp from 0 to 255 with IF 1691183.5 Ops/sec
Clamp from 0 to 255 with unary && 1806259.8 Ops/sec
Clamp in arrow function with ternary 1130624.5 Ops/sec
Clamp in regular function with MinMax 475618.0 Ops/sec
Clamp in arrow function with IF 1108615.2 Ops/sec
Clamp in regular function with unary 1123868.8 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