clamp functions (v16)

Revision 16 of this benchmark created by Blake on


Description

Which is faster?

Preparation HTML

<script>
  function clamp1(x,a,b){return x<a?a:x>b?b:x}
  function clamp2(x,a,b){return Math.min(Math.max(x,a),b)}
  function clamp(value, min, max) {
          min = min === undefined ? Number.NEGATIVE_INFINITY : min;
          max = max === undefined ? Number.POSITIVE_INFINITY : max;
          return clamp1(value, min, max);
        }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using ? :
clamp1(4,2,7);
ready
using Math object
clamp2(4,2,7);
ready
clamp
clamp(4,2,7)
ready

Revisions

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