<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
const getRandomChar = () => String.fromCharCode(Math.floor(Math.random() * 26) + 97);
const getRandomString = () => Array(Math.floor(Math.random() * 10) + 1).fill().map(getRandomChar).join('');
var values = Array(1000000).fill().map(getRandomString);
var valueToFind = values[650249];
const nativeFoundValue = values.find((val) => val === valueToFind);
const lodashFoundValue = _.find(values, (val) => val === valueToFind);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native Array.find | |
Lodash _.find |
Test name | Executions per second |
---|---|
Native Array.find | 267867.5 Ops/sec |
Lodash _.find | 263856.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is tested?
The provided JSON represents two test cases: Native Array.find and Lodash _.find. These tests compare the speed of the native JavaScript Array.find method with the equivalent method from the popular utility library, Lodash.
Options compared:
There are only two options being compared:
Pros and cons:
Native Array.find:
Pros:
Cons:
Lodash _.find:
Pros:
Cons:
Library consideration:
In this case, Lodash _.find uses a more functional programming style, which can be beneficial for readability and maintainability. However, the added overhead of using an external library may impact performance.
Special JS feature or syntax:
There are no special JavaScript features or syntax used in these tests. They rely on standard JavaScript language features.
Other alternatives:
If you're looking for alternative methods to find elements in arrays, some popular options include:
Keep in mind that these alternatives may have different performance characteristics or trade-offs compared to the native Array.find and Lodash _.find methods.