jquery throttle methods (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>
  var throttle1 = (function () {
    return function (fn, delay) {
      delay || (delay = 100);
      var last = +new Date;
      return function () {
        var now = +new Date;
        if (now - last > delay) {
          fn.apply(this, arguments);
          last = now;
        }
      };
    };
  })();
  
  var throttle2 = (function(){
      return function(fn, delay) {
          delay || (delay = 100);
          var throttle = false;
          function timeout(){ throttle = false; }
          return function(){
              if (throttle) { return; }
              throttle = setTimeout(timeout, delay);
              fn.apply(this, arguments);
          };
      };
  })();
  
  var throttle3 = (function(){
      return function(fn, delay) {
          delay || (delay = 100);
          var throttle = false;
          return function(){
              if (throttle) { return; }
              throttle = setTimeout(function(){ throttle = false; }, delay);
              fn.apply(this, arguments);
          };
      };
  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
throttle1
throttle1(function(){ return true; });
ready
throttle2
throttle2(function(){ return true; });
ready
throttle3
throttle3(function(){ return true; });
ready

Revisions

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