<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
var a = [{name: 'hello', something: 1}, {name: 'a', something: 1}, {name: 'bc', something: 1}];
var b = a.find(item => item.name === 'bc');
var a = [{name: 'hello', something: 1}, {name: 'a', something: 1}, {name: 'bc', something: 1}];
var b = _.find(a, item => item.name === 'bc');
var a = [{name: 'hello', something: 1}, {name: 'a', something: 1}, {name: 'bc', something: 1}];
var b = _.find(a, {name: 'bc'});
var a = [{name: 'hello', something: 1}, {name: 'a', something: 1}, {name: 'bc', something: 1}];
var b = _.find(a, ['name', 'bc']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
_.find | |
_.find (Object) | |
_.find (Array) |
Test name | Executions per second |
---|---|
array find | 54395544.0 Ops/sec |
_.find | 4133777.8 Ops/sec |
_.find (Object) | 2706837.8 Ops/sec |
_.find (Array) | 2072520.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark is designed to compare four different approaches for finding an element in an array:
_.find()
: A function from the Lodash library that finds the first element in an array that satisfies a predicate..find()
: A built-in JavaScript method that does the same thing as _.find()
..find()
with object literal: Similar to .find()
, but uses an object literal instead of a function..find()
with array literal: Similar to .find() with object literal
, but uses an array literal instead.Options Compared
The benchmark compares the performance of these four approaches on an array of objects, where each object has a name
property that we want to search for. The options are compared in terms of their execution time per iteration.
Pros and Cons of Each Approach
.find()
: This approach is simple and easy to read, but it can be slower than the others because it requires creating an iterator object._.find()
: This approach is optimized for performance and uses a more efficient algorithm than .find()
..find()
with object literal: This approach is similar to .find()
, but uses an object literal instead of a function. It can be slower because the JavaScript engine needs to parse the object literal..find()
with array literal: This approach is similar to .find()
with object literal, but uses an array literal instead. It can also be slower due to parsing.Libraries Used
The benchmark uses Lodash (version 4.17.5) as a dependency. Specifically, it uses the lodash.core
module for the _
alias.
Special JS Features/Syntax
There are no special JavaScript features or syntax used in this benchmark.
Other Considerations
.find()
and _.find()
are generally faster than the other two approaches.find()
with object literal and array literal approaches can be slower due to parsing.Alternatives
If you're interested in exploring alternative approaches, here are some options:
Array.prototype.findIndex()
instead of _.find()
. This method is similar but returns -1
if no element is found..find()
or Lodash methods. This can be slower and more verbose.I hope this explanation helps you understand the MeasureThat.net benchmark!