math.clamp (v14)

Revision 14 of this benchmark created by idbrii on


Preparation HTML

<script>
var gNumValues = 100;
// randomly generated gNumValues length array 0 <= A[gNumValues] <= 1000
var gTestValues = Array.from({length: gNumValues}, () => Math.floor(Math.random() * 1000 + 1));
</script>

Setup

var min = 100;
  var max = 300;
  
  
  var value = gTestValues[this.count % gNumValues];
  var result = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
if/else
if (value > max) {
  result = max;
} else if (value < min) {
  result = min;
}
ready
min/max
result = Math.min(max, Math.max(min, value));
ready
ternary clamp
result = value > max ? max : (value < min ? min : value);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Jonny Brannum on
  • Revision 2: published on
  • Revision 9: published by Maik Merten on
  • Revision 14: published by idbrii on