1. find

The first function we will illustrate is the one that we use often. Let’s try to find the first element in a collection that satisfies a condition

_.find vs find

So, this is a very simple example and let’s investigate the performance of both these functions:

array find is way performant than _.find

You can inspect the benchmark code and play with variations using the playground below:

This is not to conclude that native functions are always performant over their Lodash counterparts. There are some functions that are complicated enough that it is possible that we write an implementation that is under-performant than what the library offers. But, when there is a choice of the native function being simple and more readable, you should definitely consider it as a better alternative.

2. filter

array.filter helps you extract all elements from a collection that satisfy a specific condition

_.filter vs array.filter

filter performance comparison

3. first and rest

There will many use-cases to get the first element in a collection and dump the rest to another.

first and rest using native spread operator

You would have probably used the spread operator ... in other contexts. Here, we are using it to de-structure array elements.

The performance benefits of first and rest are left as an exercise to the reader.

4. each

You are better off using a vanilla for loop than using any of the built-in iterators. This is one such use-cases where using lodash functions pay off.

_.each vs forEach vs map

The benchmark results look interesting:

_.each is the clear emergent winner!

Lodash’s each function is much faster because of the implementation decisions taken by the library that are browser specific.

The lo-dash developers explain that the relative speed of the native forEach varies among browsers. Just because forEach is native does not mean that it is faster than a simple loop built with for or while . For one thing, the forEach has to deal with more special cases. Secondly, forEach uses callbacks, with the (potential) overhead of function invocation etc.

5. every

This function tests whether all elements in the array satisfied a specific condition. And the native version of it much faster!

_.every vs array.every

6. some

This function tests whether at least one of the elements in the array satisfied a specific condition

array.some vs _.some

7. includes

Check whether an element is contained or included in a collection.

check presence of an element in array

array.includes vs _.includes

8. uniq

Find unique elements in an array

We will be leverage the javascript’s Set data structure to convert an array to a set and convert it back to an array using the spread operator. Now, let’s see if it helping with performance to go through two conversions.

_.uniq is more performant

Now, there is a better way to filter the elements using the following snippet

elements.filter((v, i, a) => a.indexOf(v) === i)// definition: filter(callback(value, index, array)

Whether to use native functions or the lodash version is for you to decide…

9. compact

This is a useful function to remove falsey or undefined values from an array.