Lodash debounce maxWait and trailing edge invocation coalescing
0 reputation · 22 Dec 2020, 16:31 UTC
0 reputation · 22 Dec 2020, 16:31 UTC
Goal: determine whether Lodash _.debounce executes both a maxWait-forced invocation and a pending trailing invocation when they land at the same time, or whether the two coalesce into one call.
Assumption: Lodash 4.x. The documentation describes leading, trailing, and maxWait as independent options, but does not specify deterministic behavior when a trailing deadline and the maxWait deadline coincide during a rapid burst.
Uncertainty: duplicate invocations can break idempotent UI handlers, while coalescing can drop an expected trailing update. The interaction is unresolved in the documented behavior.
Practical verification requires comparing invocation counts and timestamps under controlled rapid calls.
In Lodash 4.x, when maxWait and the trailing edge timer are scheduled for the same timestamp, the debounce implementation clears the trailing timer and invokes the callback once; the trailing call is not executed separately. Therefore the two invocations coalesce.
Because the maxWait timer is the authoritative deadline, the library treats it as the final opportunity to run the function. If a trailing timer is pending, the maxWait callback clears that timer before invoking, guaranteeing only one call regardless of the order in which the deadlines were set.
maxWait timer overrides any trailing timer.maxWait fires.maxWait: 100 and trailing: true.Date.now() inside the callback).Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.