YUI 3: Y.throttle()

Benchmark created by Ryan Grove on


Preparation HTML

<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>

<script>
  var Y = YUI().use('yui-throttle', function (Y) {
  
      Y.newThrottle = function (fn, duration, context) {
          var lastTime = 0,
              lastArgs, timeout;
  
          duration || (duration = Y.config.throttleTime || 150);
  
          function execute() {
              var args = lastArgs;
  
              lastArgs = null;
              lastTime = Y.Lang.now();
              timeout  = null;
  
              fn.apply(context, args);
          }
  
          return function () {
              var now;
  
              lastArgs = arguments;
  
              if (timeout) { return; }
  
              now = Y.Lang.now();
  
              if (now - lastTime > duration) {
                  lastArgs = null;
                  lastTime = now;
  
                  fn.apply(context, arguments);
              } else {
                  timeout = setTimeout(execute, duration);
              }
          };
      };
  
  });
  
  var result;
</script>

Setup

var newThrottled = Y.newThrottle(function (n) { result = Math.random() + n; }, 200),
    
        oldThrottled = Y.throttle(function (n) { result = Math.random() + n; }, 200);

Teardown


    console.log(result);
  

Test runner

Ready to run.

Testing in
TestOps/sec
New Y.throttle()
newThrottled(Math.random());
ready
Old Y.throttle()
oldThrottled(Math.random());
ready

Revisions

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

  • Revision 1: published by Ryan Grove on