debounce

JavaScript performance comparison

Test case created by Rhys Brett-Bowen

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>
<script>
 var lodash = _.noConflict();
</script>
<script src="https://rawgithub.com/lodash/lodash/359c8fa5aa3f952b04dd15977c670e30cc3c304e/dist/lodash.js"></script>
<script>
 var lodashEdge = _.noConflict();
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var debounce = function(func, wait, immediate) {
      var timeout, args, context, timestamp, result;
      return function() {
        context = this;
        args = arguments;
        timestamp = new Date();
        var later = function() {
          var last = (new Date()) - timestamp;
          if (last < wait) {
            timeout = setTimeout(later, wait - last);
          } else {
            timeout = null;
            if (!immediate) result = func.apply(context, args);
          }
        };
        var callNow = immediate && !timeout;
        if (!timeout) {
          timeout = setTimeout(later, wait);
        }
        if (callNow) result = func.apply(context, args);
        return result;
      };
    };
  
  var fn = function(){},
      underscoreD = _.debounce(fn,100),
      lodashD = lodash.debounce(fn,100),
      lodashEdgeD = lodashEdge.debounce(fn,100),
      myD = debounce(fn,100);

};
</script>

Preparation code output

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script> <script> var lodash = _.noConflict(); </script> <script src="https://rawgithub.com/lodash/lodash/359c8fa5aa3f952b04dd15977c670e30cc3c304e/dist/lodash.js"></script> <script> var lodashEdge = _.noConflict(); </script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
underscore
underscoreD();
pending…
lodash
lodashD();
pending…
lodash (edge)
lodashEdgeD();
pending…
my
myD();
pending…

Compare results of other browsers

Revisions

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

0 Comments