Skip to content

Lodash - Function

Lodash provides powerful utilities for working with functions, including techniques like currying, partial application, memoization, and controlling function execution. While modern JavaScript offers features like arrow functions, rest/spread syntax, and Function.prototype.bind, Lodash methods can still offer enhanced capabilities or more concise syntax for certain patterns.

This section covers key Lodash methods for function manipulation and enhancement.

Method & SyntaxDescription & Modern JS Comparison
bind
_.bind(func, thisArg, [partials])
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives. Similar to Function.prototype.bind, but Lodash’s _.bind allows placeholder arguments.
partial
_.partial(func, [partials])
Creates a function that invokes func with partials prepended to the arguments it receives. Useful for creating specialized versions of functions. Can be achieved with Function.prototype.bind(null, ...partials) or arrow function closures.
partialRight
_.partialRight(func, [partials])
Like _.partial, but prepends partials to the arguments from the right.
curry
_.curry(func, [arity=func.length])
Creates a function that accepts arguments of func one at a time. If func requires more arguments, it returns a new curried function waiting for the rest. Useful in functional programming.
curryRight
_.curryRight(func, [arity=func.length])
Like _.curry, but arguments are applied from right to left.
memoize
_.memoize(func, [resolver])
Creates a function that caches the results of func calls. Subsequent calls with the same arguments return the cached result. Very useful for optimizing expensive computations. The optional resolver determines the cache key.
once
_.once(func)
Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation.
before
_.before(n, func)
Creates a function that invokes func as long as it’s called less than n times. Subsequent calls return the result of the last func invocation.
delay
_.delay(func, wait, [args])
Invokes func after wait milliseconds. Any additional arguments are provided to func when it’s invoked. Similar to setTimeout, but often used for its cleaner syntax within Lodash chains.
flip
_.flip(func)
Creates a function that invokes func with its first two arguments reversed.
negate
_.negate(predicate)
Creates a function that negates the result of the predicate function.
overArgs
_.overArgs(func, [transforms])
Creates a function that invokes func with its arguments transformed by corresponding transforms functions.
rearg
_.rearg(func, indexes)
Creates a function that invokes func with arguments arranged according to the specified indexes.
rest
_.rest(func, [start=func.length-1])
Creates a function that invokes func with the this binding of the created function and arguments from start onwards collected into an array. Largely superseded by native rest parameter syntax (...args).
spread
_.spread(func, [start=0])
Creates a function that invokes func with the this binding of the create function and an array of arguments much like Function.prototype.apply. Largely superseded by native spread syntax (...args).
unary
_.unary(func)
Creates a function that accepts up to one argument, ignoring any additional arguments.
wrap
_.wrap(value, [wrapper=identity])
Creates a function that provides value to the wrapper function as its first argument. Any additional arguments passed to the created function are provided to wrapper.
import _ from 'lodash-es';
let calculations = 0;
// Simulate an expensive function
function expensiveCalculation(n) {
calculations++;
console.log(`Calculating for ${n}...`);
// Complex logic here...
return n * n;
}
// Create a memoized version
const memoizedCalc = _.memoize(expensiveCalculation);
console.log(memoizedCalc(5)); // Output: Calculating for 5... 25
console.log(memoizedCalc(5)); // Output: 25 (Result retrieved from cache)
console.log(memoizedCalc(10)); // Output: Calculating for 10... 100
console.log(memoizedCalc(5)); // Output: 25 (Cache hit again)
console.log(memoizedCalc(10)); // Output: 100 (Cache hit again)
console.log(`Total calculations performed: ${calculations}`); // Output: Total calculations performed: 2

Lodash function methods are particularly valuable in functional programming paradigms, for performance optimization (memoization), and for creating highly reusable and configurable function logic.

Refer to the Lodash documentation for more advanced use cases, such as custom resolvers for _.memoize or using placeholders with _.bind and _.partial.