Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes vs for with let
(version: 0)
Comparing performance of:
indexOf vs includes vs lodash vs for
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
indexOf
array.indexOf('sausage') !== 1
includes
array.includes('sausage')
lodash
_.includes(array, 'sausage')
for
const inArray = (target = 'sausage', array) => { for(let i = 0; i < array.length; i++) { if(array[i] === target) { return true; } } return false; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
includes
lodash
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases. **Benchmark Definition** The benchmark measures the performance of different methods to find an element in an array: 1. `indexOf`: Returns the index of the specified value in the array, or -1 if it is not found. 2. `includes`: Returns a boolean indicating whether an element with the specified value exists in the array. 3. `_lodash.includes` (using the Lodash library): Similar to `includes`, but implemented as part of the Lodash library. 4. `for` loop: A custom implementation using a traditional `for` loop. **Options Compared** The benchmark compares the performance of these four options: * **Pros and Cons**: + `indexOf`: Simple and widely supported, but may be slower due to its use of indexing and bounds checking. + `includes`: Similar simplicity to `indexOf`, but with a more efficient implementation in modern browsers. However, it may not work correctly for certain edge cases or older browsers. + `_lodash.includes`: Part of the Lodash library, which provides a comprehensive set of utility functions. This option is likely to be slower due to the overhead of the library, but offers a convenient and flexible way to perform string matching. + `for` loop: A custom implementation using a traditional `for` loop, which requires manual iteration over the array elements. This option is likely to be the slowest, as it does not take advantage of optimized array operations or caching. **Library Used** The Lodash library is used in the `_lodash.includes` test case. * **Purpose**: The Lodash library provides a collection of reusable functions for various tasks, including string matching and iteration. In this benchmark, `_.includes` is used as a convenient way to perform string matching without having to implement it from scratch. **Special JS Feature or Syntax** None of the options in the benchmark require special JavaScript features or syntax beyond what is commonly supported by modern browsers. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Array.prototype.some()`: This method returns `true` if at least one element in the array passes a test, and `false` otherwise. While not exactly equivalent to `includes`, it can be used as a similar optimization. * **Bitwise operations**: Some browsers support using bitwise operations (e.g., `&` or `|`) to perform string matching quickly. However, these approaches are generally less readable and may require more expertise to implement correctly. Keep in mind that the performance differences between these options may not be dramatic, especially for small arrays. The benchmark is likely designed to highlight any significant performance differences between these approaches.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes vs lodash includes
IndexOf vs Includes vs lodash includes test2
IndexOf vs Includes vs lodash includes for string
array IndexOf vs array Includes vs lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?