<script src="lodash.js"></script>
var array = [Array(100000).keys()];
var rum = Math.floor(100000 * Math.random());
array.find(n => n === rum)
_.find(array,n => n === rum)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.find | |
Lodash find |
Test name | Executions per second |
---|---|
Array.prototype.find | 34191.8 Ops/sec |
Lodash find | 37755.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Overview
The benchmark compares two approaches to find an element in an array: Array.prototype.find
(native JavaScript) and _.find
from the Lodash library.
Native JavaScript Approach (Array.prototype.find
)
find()
method on the Array.prototype
, which returns the first element that satisfies the provided condition.n === rum
condition.Pros:
Cons:
find()
can vary across different browsers and environments.Lodash Approach (_.find
)
_.find()
function is a utility function that searches for the first element in an array that satisfies a given condition.Pros:
find()
function's behavior by passing custom options or modifying the underlying algorithm.Cons:
Special considerations
n === rum
condition is used to find the element that satisfies the given equality check. This is a simple yet effective way to test the performance of finding an exact match.Other alternatives
If you wanted to compare other approaches, some possible alternatives could be:
Array.prototype.indexOf()
instead of find()
.find()
without relying on a library.Keep in mind that adding more test cases would require increasing the benchmark's complexity and potentially leading to more variability in results.