Lodash Debounce Configuration Options Guide

The _.debounce method in the Lodash JavaScript library limits the rate at which a function can fire, making it essential for handling events like scrolling, resizing, and rapid user input. Beyond setting a simple delay in milliseconds, Lodash allows you to fine-tune execution behavior by passing an optional configuration object as the third argument: _.debounce(func, [wait=0], [options={}]). This article covers the specific options you can pass to configure _.debounce—namely leading, maxWait, and trailing—to control precisely when your debounced function executes.

The options Object

The third parameter of _.debounce accepts an object with three optional properties:

const debouncedFunction = _.debounce(calculateLayout, 300, {
  leading: false,
  maxWait: 1000,
  trailing: true
});

1. leading (boolean)

2. trailing (boolean)

3. maxWait (number)

Common Configuration Scenarios