Script Preparation code:
x
 
var number = Math.random() * 1000;
var min = Math.random() * 300;
var max = Math.random() * 1200;
function ternaryClamp(number, min, max) {
  return number > max ? max : (number < min ? min : number);
}
function ternaryMin(number, min) {
  return number < min ? number: min;
}
function ternaryMax(number, max) {
  return number > max ? number: max;
}
Tests:
  • Math.min/max

     
    Math.min(Math.max(number, min), max)
  • Math.min

     
    Math.min(number, min)
  • Math.max

     
    Math.max(number, max)
  • Ternary clamp

     
    ternaryClamp(number, min, max);
  • Ternary min

     
    ternaryMin(number, min)
  • Ternary max

     
    ternaryMax(number, max)
  • Ternary clamp inlined

     
    number > max ? max : (number < min ? min : number)
  • Ternary min inlined

     
    number < min ? number: min
  • Ternary max inlined

     
    number > max ? number: max
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Math.min/max
    Math.min
    Math.max
    Ternary clamp
    Ternary min
    Ternary max
    Ternary clamp inlined
    Ternary min inlined
    Ternary max inlined

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 22 days ago)
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Chrome 128 on Chrome OS 14541.0.0
View result in a separate tab
Test name Executions per second
Math.min/max 14949082.0 Ops/sec
Math.min 20418864.0 Ops/sec
Math.max 19264802.0 Ops/sec
Ternary clamp 17589622.0 Ops/sec
Ternary min 18996174.0 Ops/sec
Ternary max 21272432.0 Ops/sec
Ternary clamp inlined 17180900.0 Ops/sec
Ternary min inlined 19431834.0 Ops/sec
Ternary max inlined 20903712.0 Ops/sec