<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
var a = ['hello', 'a', 'bc'];
var b = a.find(item => item === 'bc');
var a = ['hello', 'a', 'bc'];
var b = _.find(a, item => item === 'bc');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
_.find |
Test name | Executions per second |
---|---|
array find | 73097848.0 Ops/sec |
_.find | 21406964.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The benchmark compares two approaches to achieve the same goal:
...
) used with the Array.prototype.find()
method.concat()
method in combination with the Array.prototype.includes()
method.Options Compared
find()
on that copy.concat()
method with includes()
: uses the concat()
method to create a new array and then calls includes()
on that array to find the element.Library Used
lodash
: a popular JavaScript utility library that provides functions like find()
, which is used in one of the test cases.Special JS Feature/Syntax
None mentioned in this benchmark. However, it's worth noting that the spread operator was introduced in ES6 and has become a standard feature in modern JavaScript.
Other Alternatives
There are other ways to achieve the same goal without using concat()
or the spread operator:
Array.prototype.indexOf()
: finds the index of the element in the array.Array.prototype.includes()
: checks if the element is present in the array, but returns the entire array as an argument.However, these alternatives are less concise and may not be as efficient as the two options being compared in this benchmark.
Benchmark Preparation Code
The preparation code includes a script tag that loads the lodash
library, which is used in one of the test cases.
I hope this explanation helps you understand what's being tested in this benchmark!