_shortOut.js
 1  /** Used to detect hot functions by number of calls within a span of milliseconds. */
 2  var HOT_COUNT = 800,
 3      HOT_SPAN = 16;
 4  
 5  /* Built-in method references for those with the same name as other `lodash` methods. */
 6  var nativeNow = Date.now;
 7  
 8  /**
 9   * Creates a function that'll short out and invoke `identity` instead
10   * of `func` when it's called `HOT_COUNT` or more times in `HOT_SPAN`
11   * milliseconds.
12   *
13   * @private
14   * @param {Function} func The function to restrict.
15   * @returns {Function} Returns the new shortable function.
16   */
17  function shortOut(func) {
18    var count = 0,
19        lastCalled = 0;
20  
21    return function() {
22      var stamp = nativeNow(),
23          remaining = HOT_SPAN - (stamp - lastCalled);
24  
25      lastCalled = stamp;
26      if (remaining > 0) {
27        if (++count >= HOT_COUNT) {
28          return arguments[0];
29        }
30      } else {
31        count = 0;
32      }
33      return func.apply(undefined, arguments);
34    };
35  }
36  
37  module.exports = shortOut;