<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
var users = [
{ 'user': 'joey', 'age': 32 },
{ 'user': 'ross', 'age': 41 },
{ 'user': 'chandler', 'age': 39 }
]
users.find(x => x === { 'user': 'joey', age: 32 });
_.find(users, { 'user': 'joey', age: 32 });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
lodash find |
Test name | Executions per second |
---|---|
array find | 14105181.0 Ops/sec |
lodash find | 4904574.0 Ops/sec |
What is being tested?
The provided benchmark measures the performance difference between two approaches: using native JavaScript (find
) and utilizing the popular JavaScript library Lodash's _.find
method for finding an element in an array based on equality comparison.
Options compared:
Two options are being compared:
find
): This approach uses the built-in Array.prototype.find()
method, which searches through an array and returns the first element that satisfies the provided testing function._.find
: This approach uses the Lodash library to search for an element in the array based on a predicate function.Pros and Cons:
find
):_.find
:Library usage:
The _.find
method from Lodash is being tested. Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and more. In this case, it's used to provide a convenient and efficient way to search arrays based on a predicate function.
Special JS feature or syntax:
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is solely on comparing the performance of native JavaScript (find
) versus Lodash's _.find
method for array equality-based searches.
Other alternatives:
For similar use cases, other alternatives to Lodash's _.find
could be:
every()
, but returns as soon as the first matching element is found, which might be more suitable for array search scenarios.It's worth noting that there are other libraries or approaches available for array manipulation and searching, such as Ramda, Underscore.js, or even native implementations like Array.prototype.indexOf()
with a predicate function. The choice of implementation often depends on the specific requirements, performance needs, and personal preferences of the developer.