Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Chrome 97
Mac OS X 10.15.7
Desktop
3 years ago
Test name Executions per second
Math.max/min 4051622.5 Ops/sec
if 925426112.0 Ops/sec
ternary 926729408.0 Ops/sec
Script Preparation code:
AخA
 
this.number = Math.random() * 1000;
Tests:
  • Math.max/min

     
    return Math.max(250, Math.min(750, this.number));
  • if

     
    var number = this.number;
    if(number < 250) number = 250;
    if(number > 750) number = 750;
    return number;
  • ternary

     
    var number = this.number;
    const max = (number > 750 ? 750 : number);
    return number < 250 ? 250 : max;