clamp functions (v3)

Revision 3 of this benchmark created on


Description

Which is faster?

Preparation HTML

<script>
(function(global){
  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)}
  global.clamp1 = clamp1;
  global.clamp2 = clamp2;
  global.clamp3 = clamp3;
})(window)
</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
clamp3(4,2,7);
ready

Revisions

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