clamp functions (v2)

Revision 2 of this benchmark created on


Description

Which is faster?

Preparation HTML

<script>
  var min = Math.min, max = Math.max;

  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 clamp3(x,a,b){return min(max(x,a),b)}
</script>

Test runner

Ready to run.

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

Revisions

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