<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 }
]
// Native
users.find(function (o) { return o.age < 40; })
_.find(users, function (o) { return o.age < 40; })
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
_.find |
Test name | Executions per second |
---|---|
array find | 63061552.0 Ops/sec |
_.find | 34723164.0 Ops/sec |
Let's break down the benchmark and explain what is being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The benchmark compares two approaches: using the native find
method in JavaScript with the spread operator (...
) versus using the _find
function from the Lodash library. The goal is to determine which approach is faster for finding an element within an array based on a certain condition.
Native Find vs Lodash _.find
The native find
method uses the following syntax:
array.find(callback)
Where callback
is a function that returns the desired element or undefined
.
The Lodash _find
function uses the following syntax:
_.find(array, callback)
Both functions take two arguments: array
and callback
. The callback
function takes an element from the array as an argument and returns the desired element.
Options Compared
There are two main options being compared:
find
method with the spread operator (...
).const result = users.find(function (o) { return o.age < 40; });
_find
function from Lodash.const result = _.find(users, function (o) { return o.age < 40; });
Pros and Cons
Native Find:
Pros:
Cons:
Lodash _.find:
Pros:
Cons:
Other Considerations
...
) with native find
is a relatively new feature that may not be supported by all browsers.Library Used: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and more. In this benchmark, Lodash's _find
function is used to find an element within an array based on a condition.
Special JS Feature or Syntax
There are no special JS features or syntaxes being tested in this benchmark. The focus is solely on comparing the performance of two approaches: native find
with spread operator versus Lodash _.find.