From 135cd7f3830b2c33a2fac7e33bbd513097f7c90f Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Fri, 3 Apr 2026 10:10:15 +0900 Subject: [PATCH 1/2] fix(compat/function/debounce): skip 'maxWait' invocation when both 'leading' and 'trailing' are 'false' --- src/compat/function/debounce.spec.ts | 24 ++++++++++++++++++++++++ src/compat/function/debounce.ts | 5 ++++- src/compat/function/throttle.spec.ts | 24 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/compat/function/debounce.spec.ts b/src/compat/function/debounce.spec.ts index 0817a55543..2042bd37a8 100644 --- a/src/compat/function/debounce.spec.ts +++ b/src/compat/function/debounce.spec.ts @@ -559,6 +559,30 @@ describe('debounce', () => { expect(callCount).toBe(0); }); + it('should not invoke the function with `maxWait` when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const debounced = debounce( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false, maxWait: 64 } + ); + + debounced(); + debounced(); + + await delay(96); + + debounced(); + debounced(); + + await delay(96); + + expect(callCount).toBe(0); + }); + it('should match the type of lodash', () => { expectTypeOf(debounce).toEqualTypeOf(); }); diff --git a/src/compat/function/debounce.ts b/src/compat/function/debounce.ts index 559985b3be..b8248b5ddf 100644 --- a/src/compat/function/debounce.ts +++ b/src/compat/function/debounce.ts @@ -198,7 +198,10 @@ export function debounce any>( } if (Date.now() - pendingAt >= maxWait) { - result = func.apply(this, args); + if (leading || trailing) { + result = func.apply(this, args); + } + pendingAt = Date.now(); _debounced.cancel(); diff --git a/src/compat/function/throttle.spec.ts b/src/compat/function/throttle.spec.ts index 93d05ceb32..41914e5871 100644 --- a/src/compat/function/throttle.spec.ts +++ b/src/compat/function/throttle.spec.ts @@ -353,6 +353,30 @@ describe('throttle', () => { expectTypeOf(throttle).toEqualTypeOf(); }); + it('should not invoke the function when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const throttled = throttle( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false } + ); + + throttled(); + throttled(); + + await delay(96); + + throttled(); + throttled(); + + await delay(96); + + expect(callCount).toBe(0); + }); + it('should not invoke the function even after flush is called if timer is going', async () => { let callCount = 0; const throttled = throttle(() => ++callCount, 32); From 2d6be6612655db4acf378b6b8d1d9ea4ae9c6b86 Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Mon, 17 Aug 2026 17:08:31 +0900 Subject: [PATCH 2/2] test: update bundle size snapshots --- benchmarks/bundle-size/debounce.spec.ts | 2 +- benchmarks/bundle-size/throttle.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/bundle-size/debounce.spec.ts b/benchmarks/bundle-size/debounce.spec.ts index ac3dcbbf95..f731126385 100644 --- a/benchmarks/bundle-size/debounce.spec.ts +++ b/benchmarks/bundle-size/debounce.spec.ts @@ -14,6 +14,6 @@ describe('debounce bundle size', () => { it('es-toolkit/compat', async () => { const bundleSize = await getBundleSize('es-toolkit/compat', 'debounce'); - expect(bundleSize).toMatchInlineSnapshot(`961`); + expect(bundleSize).toMatchInlineSnapshot(`971`); }); }); diff --git a/benchmarks/bundle-size/throttle.spec.ts b/benchmarks/bundle-size/throttle.spec.ts index b7e81b7c43..0ea3c5cb24 100644 --- a/benchmarks/bundle-size/throttle.spec.ts +++ b/benchmarks/bundle-size/throttle.spec.ts @@ -14,6 +14,6 @@ describe('throttle bundle size', () => { it('es-toolkit/compat', async () => { const bundleSize = await getBundleSize('es-toolkit/compat', 'throttle'); - expect(bundleSize).toMatchInlineSnapshot(`1065`); + expect(bundleSize).toMatchInlineSnapshot(`1075`); }); });