Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / setData.js
1 var baseSetData = require('./baseSetData'),
2     now = require('../date/now');
3
4 /** Used to detect when a function becomes hot. */
5 var HOT_COUNT = 150,
6     HOT_SPAN = 16;
7
8 /**
9  * Sets metadata for `func`.
10  *
11  * **Note:** If this function becomes hot, i.e. is invoked a lot in a short
12  * period of time, it will trip its breaker and transition to an identity function
13  * to avoid garbage collection pauses in V8. See [V8 issue 2070](https://code.google.com/p/v8/issues/detail?id=2070)
14  * for more details.
15  *
16  * @private
17  * @param {Function} func The function to associate metadata with.
18  * @param {*} data The metadata.
19  * @returns {Function} Returns `func`.
20  */
21 var setData = (function() {
22   var count = 0,
23       lastCalled = 0;
24
25   return function(key, value) {
26     var stamp = now(),
27         remaining = HOT_SPAN - (stamp - lastCalled);
28
29     lastCalled = stamp;
30     if (remaining > 0) {
31       if (++count >= HOT_COUNT) {
32         return key;
33       }
34     } else {
35       count = 0;
36     }
37     return baseSetData(key, value);
38   };
39 }());
40
41 module.exports = setData;