clamp functions

Benchmark created 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)}
  
</script>

Test runner

Ready to run.

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

Revisions

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