๐ŸŒ€

My favorite Lodash Functions

April 06, 2020ยท๐Ÿ“š 5 min read
Photo By Iker Urteaga

Lodash is a fantastic library of JavaScript utilities that can really enhance your productivity and confidence as a developer. But with things being added to JavaScript every year, is Lodash still relevant?

Sometimes it's easy to question the relevancy of Lodash with things like:

Heck, there's even a proposal for a JavaScript standard library that basically aims to deprecate lodash!

Lodash is still very helpful in everyday use. I've used these lodash utilities throughout my career and will probably continue to use them until TC39 adds their functionality to ECMAScript.

TLDR;

  1. _.debounce
  2. _.throttle
  3. _.range
  4. _.memoize
  5. _.orderBy
  6. _.isEqual
  7. _.pick
  8. _.repeat
  9. _.truncate
  10. _.capitalize

Debounce โ›น๐Ÿปโ€โ™‚๏ธ

_.debounce

Groups some number of repeated actions that occur during a time frame.

This is really nice for creating a search-as-you-type input that makes an api call to display the results.

Throttle ๐Ÿš™

_.throttle

Similar to _.debounce, but instead of grouping repeated actions, it ensures that an action is only called once within a certain time frame. This is nice for performance reasons where we can get away with a delay to improve UX.

David Corbacho wrote a great article about the details of both _.debounce and _.throttle. I'd recommend giving it a read.

Range ๐Ÿšœ

_.range

Creates an array of consecutive integers, of a given length.

Almost every other language I've used has native ranges built in, and I miss them in JavaScript! This is a nice helper to create ranges that I use a TON.

Memoize ๐Ÿ’ต

_.memoize

Memoization is a caching technique used for performance.

The basic idea is that given the same input over and over, a memoized function will calculate the return value the first time, cache it, and return that same value instead of actually running the function each time.

I've used this when creating a search index for fast frontend searching with something like fuse.js. With _.memoize, the search index is only calculated once for the set of items.

You can implement your own memoization helper in vanilla JS, but there are some tricky edge cases lined out in this article, and lodash already has a battle tested one for us.

Shuffle ๐Ÿ‚ก

_.shuffle

Really pretty simple. Takes an array, and returns a new array, shuffled! I've found this useful when I want a "random" list of items.

IsEqual โœ…

_.isEqual

This is very nice to see if two objects have the same values. It deeply checks each object to determine if they have the same values.

I've used this the most to check if an array of items has changed when saving forms.

Pick โ›

_.pick

Pick only a subset of keys from an object. This is really handy when ensuring that only certain values are on an object before sending through an operation.

I've used this mostly as a safety layer before making api requests to make sure that things aren't accidentally getting sent that shouldn't.

Repeat ๐Ÿ”

_.repeat

Repeat a string n times.

This is nice when you need a dynamic length string of special characters.

I've used this when building a CLI with node and I wanted a box of * around a word.

Truncate โœ‚

_.truncate

Truncate a string when it is longer than a certain number of characters.

I've used this most when I want uniform items in a list, and have a description of some kind for each item. I'll truncate the description so that it only ever wraps 2 lines at most

Capitalize ๐Ÿ›

_.capitalize

Capitalize a word for readability. This is really nice because it includes a ton of edge cases for text formatting.


One honorable mention utility is _.get. I've used _.get() more that any of the utilites above, but recently Create React App v3 includes support for Optional Chaining, so I don't use it anymore. ๐Ÿคท๐Ÿปโ€โ™‚๏ธ

And that's it! Let me know if I missed a one that you find useful on Twitter.

About the Author

Hi ๐Ÿ‘‹ I'm Tyler. I'm a full stack software engineer with a passion for learning new things. I love solving hard problems and simplifying them down to their pieces. Presently residing in Utah with my two girls and beautiful wife.