<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
var a = [{a: 'hello'}, {a: 'a'}, {a: 'bc'}, {a: 'ad'}, {a: 'mg'}, {a: 'asd'}];
var b = a.find(item => item.a === 'bc');
var a = [{a: 'hello'}, {a: 'a'}, {a: 'bc'}, {a: 'ad'}, {a: 'mg'}, {a: 'asd'}];
var b = a.some(item => item.a === 'bc');
var a = [{a: 'hello'}, {a: 'a'}, {a: 'bc'}, {a: 'ad'}, {a: 'mg'}, {a: 'asd'}];
var b = _.some(a, ['a', 'bc']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
array some | |
lodash some |
Test name | Executions per second |
---|---|
array find | 68782600.0 Ops/sec |
array some | 71516568.0 Ops/sec |
lodash some | 7736984.5 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
What is being tested?
The provided JSON represents a microbenchmark that compares three different approaches to filter an array:
Array.prototype.find()
: This method returns the first element in the array that satisfies the provided condition.Array.prototype.some()
: This method returns true
if at least one element in the array satisfies the provided condition._
(lodash): A popular JavaScript library for functional programming utilities, including filtering functions.Options compared
The benchmark compares three options:
Array.prototype.find()
with a callback functionArray.prototype.some()
with a callback functionlodash.some()
with an array of values to filterPros and Cons
Here are some pros and cons for each approach:
Array.prototype.find()
:Array.prototype.some()
:find()
in a loop, returns as soon as it finds the first match, and is more concise.find()
returns undefined.lodash.some()
: Library usage
In this benchmark, the lodash library is used in two test cases:
Array.prototype.some()
using _.some(a, ['a', 'bc'])
lodash.some()
using var b = _.some(a, ['a', 'bc'])
Lodash provides a set of functional programming utilities, including filtering functions like some()
. The _
notation is used to access these utility functions.
Special JS feature or syntax
None of the test cases use any special JavaScript features or syntax that would require explanation beyond the basics of array methods and lodash usage.