Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs _.includes for number array
(version: 0)
Comparing performance of:
IndexOf vs Includes vs lodash
Created:
5 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 = [6, 44, 542, 235, -26, 82, 235, 277, 643, 90, 777, 334, 41, 5, 98, 183];
Tests:
IndexOf
array.indexOf(777) !== 1
Includes
array.includes(777)
lodash
_.includes(array, 777)
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
41236116.0 Ops/sec
Includes
37556968.0 Ops/sec
lodash
12575131.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's being tested in the provided benchmark, describe the options compared, their pros and cons, and other considerations. **What's being tested:** The benchmark is testing the performance of three different methods to check if a specific number exists in an array: 1. `indexOf()` 2. `includes()` 3. `_.includes()` (using Lodash library) **Options compared:** * **`indexOf()`**: This method searches for the first occurrence of a specified value in the array and returns its index. If the value is not found, it returns `-1`. * **`includes()`**: This method checks if an element exists in an array, returning `true` if found and `false` otherwise. * **`.includes()` (Lodash)**: This is a higher-order function that checks if an element exists in an array, similar to the native `includes()` method. **Pros and Cons of each approach:** * **`indexOf()`**: + Pros: Fast and efficient for small arrays, returns index if found. + Cons: Can be slow for large arrays, may return -1 if not found, can throw an error if array is modified while searching. * **`includes()`**: + Pros: Modern and widely supported, returns boolean result. May be faster than `indexOf()` for some use cases. + Cons: May not work in older browsers or environments that don't support it, can be slower than `indexOf()` for very large arrays. * **`.includes()` (Lodash)**: + Pros: Higher-order function with more flexibility, can be used as a generic utility function. Returns boolean result. + Cons: Requires Lodash library to be included, may have performance overhead compared to native methods. **Other considerations:** * The benchmark uses a fixed-size array of numbers for testing, which might not be representative of real-world scenarios where arrays can be dynamically generated or modified. * The `includes()` method is tested with both a single value and an empty array, but the latter is not used in any of the test cases. **Library usage:** Lodash's `.includes()` function is used as a benchmarking tool. Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks, such as array manipulation, string handling, and more. **Special JS feature or syntax:** None mentioned in the provided code or benchmarks. **Alternatives:** Other alternatives to these methods could include: * `Array.prototype.find()` (ECMAScript 2015+): Returns the first element that satisfies the provided testing function. * `Array.prototype.findIndex()` (ECMAScript 2015+): Similar to `indexOf()`, but returns `-1` if not found, instead of throwing an error. * Using a custom implementation for array search, such as using a binary search algorithm. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the native methods and Lodash's `.includes()` function.
Related benchmarks:
IndexOf vs Includes array of numbers
Array.indexOf vs Array.includes vs lodash includes with numerical values
array IndexOf vs array Includes vs lodash indexOf
array using indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?