<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
var selected = [{ id: '1' }, { id: '2' }, { id: '3' }];
var found = selected.find(m => m.id === '3');
var found = _.find(selected, m => m.id === '3');
var found = _.some(selected, m => m.id === '3')
var found = selected.some(m => m.id === '3')
var found = selected.findIndex(m => m.id === '3')
var indexes = selected.map(s => s.id);
var found = selected.includes('3');
var found = _.findIndex(selected, m => m.id === '3');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native find | |
Lodash find | |
Lodash some | |
Native some | |
Native find index | |
Complex | |
Lodash find index |
Test name | Executions per second |
---|---|
Native find | 11328206.0 Ops/sec |
Lodash find | 2054333.4 Ops/sec |
Lodash some | 2595023.5 Ops/sec |
Native some | 11346746.0 Ops/sec |
Native find index | 11345956.0 Ops/sec |
Complex | 5132002.5 Ops/sec |
Lodash find index | 2693133.0 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of different methods for searching an item in an array. The test cases are designed to compare the execution speed of native JavaScript methods (some
, find
, and findIndex
) with their Lodash counterparts.
Native Methods
some
: Returns true
if at least one element in the array satisfies the provided condition.find
: Returns the first element that satisfies the provided condition, or -1
if no such element is found.findIndex
: Returns the index of the first element that satisfies the provided condition, or -1
if no such element is found.Lodash Methods
_.some()
: Similar to native some
, but uses Lodash's optimized implementation._.find()
: Similar to native find
, but uses Lodash's optimized implementation._.findIndex()
: Similar to native findIndex
, but uses Lodash's optimized implementation.Comparison of Methods
The pros and cons of each method are as follows:
some
:.some()
:some
.find
:.find()
:find
.findIndex
:.findIndex()
:findIndex
.Additional Considerations
Other Alternatives
Array.prototype.includes()
: This method returns true
if the array includes the specified value, but it's only available in ECMAScript 2019 and later versions.Array.prototype.findIndex()
with Array.prototype.some()
: This combination can provide an alternative to Lodash's methods while avoiding the dependency on a third-party library.In conclusion, the choice of method depends on the specific use case and performance requirements. For simple scenarios, native methods may be sufficient, while optimized implementations like Lodash's methods or built-in Array.prototype.includes()
may offer better performance.