Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
7 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')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
173731872.0 Ops/sec
Includes
183084896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance difference between `indexOf` and `includes` methods in JavaScript, which are used to search for a specific value within an array. **Options Compared:** Two options are compared: 1. **`indexOf` method**: The `indexOf` method returns the index of the first occurrence of the specified value within the array, or -1 if the value is not found. 2. **`includes` method**: The `includes` method returns a boolean value indicating whether the array includes the specified value. **Pros and Cons:** * **`indexOf` method:** Pros: * Generally faster because it only searches for the first occurrence of the value, stopping as soon as it finds a match. * Can be more efficient when searching in large arrays where the first occurrence is likely to be relevant. * Cons: * May not return accurate results if the array contains duplicate values and you want to find all occurrences. * **`includes` method:** Pros: * Returns a boolean value, making it easier to handle cases where the value is not found. * More intuitive for certain use cases, such as searching in arrays of strings where order matters. * Cons: * Generally slower than `indexOf` because it needs to scan through the entire array. **Library and Purpose:** There are no libraries explicitly mentioned in the provided benchmark definition or test cases. However, both methods (`indexOf` and `includes`) are native JavaScript methods that don't require any external libraries for their implementation. **Special JS Feature/Syntax:** There is a special syntax used in the `includes` method. The `includes()` method introduces a new way to perform array searching using a more intuitive and concise syntax, which can improve readability and performance in certain scenarios. **Benchmark Preparation Code:** The provided JavaScript code creates an array with three elements: "banana", "sausage", and "jesus". This is used as the input for both `indexOf` and `includes` methods to measure their performance. **Other Alternatives:** Alternative methods or approaches could include: * Using a loop-based approach to search for the value in the array. * Utilizing other built-in JavaScript methods, such as `forEach()` or `every()`, which might be faster or more suitable for certain use cases. * Implementing custom binary search algorithms to improve performance for large arrays. Keep in mind that these alternatives would require additional code and potentially different benchmarking strategies.
Related benchmarks:
IndexOf vs Includes str
IndexOf vs Includes vs find
IndexOf vs Includes in array
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?