Optimizing Lodash Usage
While Lodash is generally performant, there are ways to optimize its usage for better performance.
For simple operations, native JavaScript methods can be faster:
// Lodash
_.map([1, 2, 3], n => n * 2);
// Native (faster for simple operations)
[1, 2, 3].map(n => n * 2);
Import only the methods you need to reduce bundle size:
import map from 'lodash/map';
import filter from 'lodash/filter';
_.flow()
for Function Composition_.flow()
can be more efficient than chaining for certain operations:
const transform = _.flow(
array => _.map(array, n => n * 2),
array => _.filter(array, n => n > 5)
);
transform([1, 2, 3, 4]);
// => [6, 8]
Previous: Chaining Next: ES6 and Lodash
Last updated 2 days ago