From 21db7d438bc394b6b23ef6feddbbfe1ce9b84a97 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 24 Aug 2013 00:29:56 -0700 Subject: [PATCH] Reduce the number of times `clearTimeout` is called in `_.debounce`. Former-commit-id: 5b07b6660be4ff6783a8348fc8a122cfab5d10c4 --- lodash.js | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lodash.js b/lodash.js index f3113be2b..b58861edc 100644 --- a/lodash.js +++ b/lodash.js @@ -5375,6 +5375,7 @@ function debounce(func, wait, options) { var args, result, + stamp, thisArg, callCount = 0, lastCalled = 0, @@ -5395,24 +5396,29 @@ maxWait = 'maxWait' in options && nativeMax(wait, options.maxWait || 0); trailing = 'trailing' in options ? options.trailing : trailing; } - var clear = function() { - clearTimeout(maxTimeoutId); - clearTimeout(timeoutId); - callCount = 0; - maxTimeoutId = timeoutId = null; - }; - var delayed = function() { - var isCalled = trailing && (!leading || callCount > 1); - clear(); - if (isCalled) { - lastCalled = +new Date; - result = func.apply(thisArg, args); + var remaining = wait - (new Date - stamp); + if (remaining <= 0) { + var isCalled = trailing && (!leading || callCount > 1); + callCount = 0; + timeoutId = null; + if (maxTimeoutId) { + clearTimeout(maxTimeoutId); + maxTimeoutId = null; + } + if (isCalled) { + lastCalled = +new Date; + result = func.apply(thisArg, args); + } + } else { + timeoutId = setTimeout(delayed, remaining); } }; var maxDelayed = function() { - clear(); + callCount = 0; + clearTimeout(timeoutId); + maxTimeoutId = timeoutId = null; if (trailing || (maxWait !== wait)) { lastCalled = +new Date; result = func.apply(thisArg, args); @@ -5421,26 +5427,24 @@ return function() { args = arguments; + stamp = +new Date; thisArg = this; callCount++; - // avoid issues with Titanium and `undefined` timeout ids - // https://github.com/appcelerator/titanium_mobile/blob/3_1_0_GA/android/titanium/src/java/ti/modules/titanium/TitaniumModule.java#L185-L192 - clearTimeout(timeoutId); - if (maxWait === false) { if (leading && callCount < 2) { result = func.apply(thisArg, args); } } else { - var stamp = +new Date; if (!maxTimeoutId && !leading) { lastCalled = stamp; } var remaining = maxWait - (stamp - lastCalled); if (remaining <= 0) { - clearTimeout(maxTimeoutId); - maxTimeoutId = null; + if (maxTimeoutId) { + clearTimeout(maxTimeoutId); + maxTimeoutId = null; + } lastCalled = stamp; result = func.apply(thisArg, args); } @@ -5448,7 +5452,7 @@ maxTimeoutId = setTimeout(maxDelayed, remaining); } } - if (wait !== maxWait) { + if (!timeoutId && wait !== maxWait) { timeoutId = setTimeout(delayed, wait); } return result;