Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf VS includes in JavaScript Array
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [4457, 6563, 30851, 40303, 42053, 47405, 52810, 90772, 96252, 125488, 151673, 158557, 178050, 195190, 203029, 210539, 216541, 242156, 262560, 275861, 284045, 285385, 286899, 307581, 337018, 340978, 353359, 365569, 398211, 398284, 424423, 425550, 427665, 429594, 440844, 445754, 452877, 491428, 494809, 501187, 513180, 518540, 520189, 527068, 532607, 537414, 541551, 543608, 544796, 548722, 564979, 569266, 586233, 589433, 604742, 628230, 649872, 651744, 657815, 663902, 669333, 676214, 684405, 705674, 706487, 709528, 728070, 728490, 729012, 741404, 742443, 750860, 760686, 783419, 791554, 797225, 799311, 813491, 816277, 822413, 844859, 865342, 869820, 871814, 873451, 873918, 879036, 882069, 902231, 918049, 918788, 920500, 930129, 932809, 943528, 943818, 964607, 974967, 995708, 999756];
Tests:
indexOf
array.indexOf(151673);
includes
array.includes(151673);
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:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
70283728.0 Ops/sec
includes
68819616.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark JSON and explain what's being tested, compared, and its implications. **Benchmark Definition** The benchmark is designed to measure the performance difference between two JavaScript methods: `indexOf` and `includes`. Both methods are used to find a specific value within an array. The script preparation code creates a large array of numbers and the test cases use this array. **Test Cases** There are two test cases: 1. **indexOf**: This test case uses the `indexOf` method to find the index of a specific number (151673) in the array. 2. **includes**: This test case uses the `includes` method to check if a specific number (151673) is present in the array. **Comparison** The comparison between `indexOf` and `includes` is based on their performance, measured by the number of executions per second. **Options Compared** Two options are being compared: 1. **Array.indexOf()**: A method that returns the index of a specified value within an array. 2. **Array.includes()**: A method that checks if a value exists in an array (including its presence at any position). **Pros and Cons of Each Approach:** * **`indexOf()`**: * Pros: * Returns the actual index of the found value, which can be useful for iterating over arrays. * Can provide early exit when the value is not present in the array, reducing unnecessary iterations. * Cons: * May perform slower due to its iterative nature and potential cache misses. * **`includes()`**: * Pros: * Faster than `indexOf()` since it uses a more efficient algorithm that skips over unnecessary elements. * Provides early exit when the value is not present in the array, reducing unnecessary iterations. * Cons: * Returns a boolean result instead of the actual index, which might be less useful for some use cases. **Library:** There is no explicit library mentioned in this benchmark. Both `indexOf` and `includes` are native JavaScript methods built into the language. **Special JS Feature or Syntax:** No special JavaScript features or syntaxes are used in these test cases. **Other Considerations:** When choosing between `indexOf` and `includes`, consider the following: * If you need to find the actual index of a value, use `indexOf()`. * If you only care about whether a value is present in the array (e.g., for faster equality checks), use `includes()`. **Alternatives:** Some alternative approaches for finding specific values within arrays include: 1. **`findIndex()`**: Returns the index of the first occurrence of the specified value, similar to `indexOf()`. 2. **Array.prototype.findIndex()`: A more concise and readable way to find the index of a value using an arrow function. 3. **Binary Search**: If you know the range of values in the array (e.g., integers between 1 and n), binary search can be more efficient than linear searches like `indexOf()` or `includes()`.
Related benchmarks:
IndexOf vs Includes vs lodash includes 1231
Array IndexOf vs includes
set.has vs. array.includes vs array.indexOf (string values) - 800 ids
array indexOf vs includes vs some w/ largeish array
Comments
Confirm delete:
Do you really want to delete benchmark?