var array = ['banana', 'sausage', 'jesus']
array.indexOf('sausage') !== 1
array.includes('sausage')
_.includes(array, 'sausage')
const inArray = (target = 'sausage', array) => {
for(var i = 0; i < array.length; i++) {
if(array[i] === target) {
return true;
}
}
return false;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf | |
includes | |
lodash | |
for |
Test name | Executions per second |
---|---|
indexOf | 10817521.0 Ops/sec |
includes | 11842122.0 Ops/sec |
lodash | 3254000.5 Ops/sec |
for | 964807488.0 Ops/sec |
I'll break down the benchmark definition and test cases to explain what's being tested, the pros and cons of different approaches, and other considerations.
Benchmark Definition JSON
The benchmark definition represents a series of tests designed to compare the performance of four different methods for searching an array:
indexOf
: The built-in JavaScript method for finding the index of a specific value in an array.includes
: A newer built-in JavaScript method (introduced in ES6) for checking if a value is present in an array.lodash includes
: A custom implementation using the _includes
function from the Lodash library, which provides various utility functions for working with arrays and objects.for
: A custom implementation using a simple loop to iterate through the array elements.Test Cases
Each test case represents one of the four methods being tested:
indexOf
method with a specific value ("sausage") in an example array.includes
method with the same value as above._includes
function from Lodash, also with the same value as above.What's being tested?
Each test case is designed to measure the performance of each method:
indexOf
, includes
, and _includes
are testing the efficiency of these built-in methods for searching arrays.for
implementation is testing the performance of a custom loop-based approach.Pros and Cons of different approaches:
_includes
:Other considerations:
Alternatives:
If you're looking for alternatives to these methods, consider:
Array.prototype.some()
or Array.prototype.every()
: These methods can be used in combination with callback functions to achieve similar results.bitwise XOR
).Keep in mind that the choice of method ultimately depends on your specific use case, performance requirements, and coding style preferences.