es-toolkit icon indicating copy to clipboard operation
es-toolkit copied to clipboard

Discrepancy between lodash and compat throttle with both trailing and leading disabled

Open DominikSerafin opened this issue 1 month ago • 0 comments

While I was sanity-checking the reproduction on this issue, I've found that there's a discrepancy between lodash and compat throttle:

import lodashThrottle from 'lodash-es/throttle';
import compatThrottle from 'es-toolkit/compat/throttle';

const WAIT = 100;
const OPTIONS = {leading: false, trailing: false};

const lodashThrottled = lodashThrottle(() => console.log('lodashThrottled'), WAIT, OPTIONS);
const compatThrottled = compatThrottle(() => console.log('compatThrottled'), WAIT, OPTIONS);

// These seem to be mostly consistent on my machine
setInterval(lodashThrottled, 1);
setInterval(compatThrottled, 1);

// Things start to get inconsistent
setInterval(lodashThrottled, 100);
setInterval(compatThrottled, 100);

// Lodash now executes far more rarely, while compat seems to execute according to delay
// The lodash timing appears to grow exponentially compared to compat
setInterval(lodashThrottled, 115);
setInterval(compatThrottled, 115);

// Note: system/browser workload seems to increase the timing inconsistencies as well

This is a bit extreme edge case considering this seems to happen only with both trailing and leading edges disabled, but configuration like this is still implicitly allowed (even though the effects of doing so are not generally established), so I thought it deserved mention.

DominikSerafin avatar Oct 22 '25 18:10 DominikSerafin