<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
var elements = ['a', 'b', 'c']
_.findIndex(elements, e => e === 'b')
elements.findIndex(e => e === 'b')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.findIndex | |
array.findIndex |
Test name | Executions per second |
---|---|
_.findIndex | 3367944.0 Ops/sec |
array.findIndex | 9518921.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Description
The website MeasureThat.net
is used to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark that compares two methods for finding an index in an array: _findIndex
from Lodash (a utility library) and array.findIndex
.
What are we testing?
We're testing the performance of these two approaches:
_findIndex
method from Lodash, which takes an array (elements
) and a callback function (e => e === 'b'
) as arguments. The callback function is expected to return true
if the element at index e
in the array matches 'b'
, and false
otherwise.findIndex
method on arrays, which also takes a callback function (e => e === 'b'
) as an argument.Pros and Cons of each approach:
Library: Lodash
Lodash is a popular utility library that provides a wide range of functions and methods for working with arrays, objects, and other data structures. The _findIndex
method is one of its many useful tools for manipulating arrays in functional programming styles. In this benchmark, we're using Lodash to test the performance of _.findIndex
, while comparing it to the native implementation on arrays.
Special JS feature: None
There are no special JavaScript features or syntax used in this benchmark that would require additional explanation beyond the standard usage of arrays and callback functions.
Other alternatives:
If you needed to find an index in an array, there are other approaches you could take:
for
loop to iterate over the elements of the array and check each one until you find the match.Array.prototype.reduce()
to accumulate an index value that increments with each iteration, stopping when the condition is met.These alternatives would likely have different performance characteristics compared to the native findIndex
method or Lodash's _findIndex
method. However, they might be more suitable for specific use cases where readability and maintainability are prioritized over raw performance.