<script src="lodash.js"></script>
const peopleArray = []
for(i=0;i<100000;i++){
peopleArray.push({name: 'Gosia', surname: 'Mazurek'})
}
peopleArray.push({name: 'Adam', surname:'Horodyski'})
var array = [peopleArray];
array.find(n => n.name === 'Adam' && n.surename === 'Horodyski')
_.find(array,n => n === 99999)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.find | |
Lodash find |
Test name | Executions per second |
---|---|
Array.prototype.find | 3065.1 Ops/sec |
Lodash find | 2371.9 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Overview
The benchmark is comparing the performance of Array.prototype.find
(a native JavaScript method) and _find
from the Lodash library, which is a popular utility library for functional programming in JavaScript. The test case uses two libraries: Lodash and its own implementation of find
, as the Array prototype does not have this method.
What is being tested?
The benchmark is testing the performance difference between using the native JavaScript Array.prototype.find
method versus the _.find
function from Lodash, when searching for an object within a large array. The test case generates a large array of 100,000 objects and adds one more element to it.
Options compared
Two options are being compared:
Array.prototype.find
: This method is implemented natively by the JavaScript engine, which means it's likely to be optimized for performance._find
function: This function uses a different algorithm and implementation than the native Array.prototype.find
, but it provides similar functionality.Pros and Cons of each approach
Array.prototype.find
:_find
function:Library usage
In this benchmark, the Lodash library is used for its _find
function. Lodash provides a wide range of utility functions for common tasks in JavaScript, such as array manipulation, object transformation, and functional programming.
Special JS feature or syntax
There are no special JavaScript features or syntaxes mentioned in the benchmark definition. The focus is on comparing the performance of two different methods: native Array.prototype.find
and Lodash's _find
function.
Now that we've explored the details of the benchmark, let's discuss alternatives:
Alternatives
If you're looking for alternative ways to search for an object within a large array, here are some options:
Array.prototype.findIndex
: This method returns the index of the first element that satisfies the provided condition, rather than the element itself.Keep in mind that these alternatives may have different performance characteristics or trade-offs depending on your specific use case.