Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes test
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = new Array(10000); array.push('sausage');
Tests:
IndexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
lodash
_.includes(array, 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash
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 what is being tested in the provided JSON and explain the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition defines three test cases: 1. `IndexOf`: Tests the `indexOf` method of arrays to find the index of a specific value. 2. `Includes`: Tests the `includes` method of arrays to check if a value is present in the array. 3. `lodash includes`: Tests the `_.includes` function from the Lodash library to include a value in an array. **Script Preparation Code** The script preparation code creates an array with 10,000 elements and pushes a single string "sausage" into it. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash JavaScript file version 4.17.5. **Options Compared** The benchmark compares three options: 1. **Native `indexOf` method**: Uses the built-in `indexOf` method of arrays in JavaScript. 2. **Native `includes` method**: Uses the built-in `includes` method of arrays in JavaScript (introduced in ECMAScript 2019). 3. **Lodash `_.includes` function**: Uses the `_.includes` function from the Lodash library. **Pros and Cons** 1. **Native `indexOf` method**: * Pros: Fast, widely supported, and well-optimized. * Cons: May not be as readable or maintainable as other options. 2. **Native `includes` method**: * Pros: Modern, efficient, and easy to read. * Cons: Requires ECMAScript 2019 support and may not work in older browsers. 3. **Lodash `_.includes` function**: * Pros: Widely used, well-maintained, and provides a fallback option for older browsers. * Cons: Adds extra overhead due to the library's complexity. **Other Considerations** * The benchmark measures the performance of each option on different devices (desktop) with Chrome 79 browser. * The results show that `includes` method is the fastest, followed by `indexOf`, and then Lodash `_.includes` function. * Using a large array size (10,000 elements) may not be representative of real-world scenarios. **Alternatives** If you need to include values in arrays, other alternatives to consider: 1. **Using `findIndex` method**: If you want to find the index of the first occurrence of a value, use the `findIndex` method instead of `indexOf`. 2. **Using `some` method**: If you want to check if any element in an array satisfies a condition, use the `some` method. 3. **Using custom implementation**: You can write your own custom function for including values in arrays, which may provide better performance or control over the implementation. Keep in mind that the choice of method depends on your specific requirements and constraints.
Related benchmarks:
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?