Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find in array
(version: 0)
Finds if a string is inside an array of strings
Comparing performance of:
includes vs indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['hello', 'hi', 'aaa'];
Tests:
includes
arr.includes('hello');
indexOf
arr.indexOf('hello') == -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
indexOf
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):
I'd be happy to explain what's being tested in the provided benchmark. **Benchmark Overview** The benchmark tests two different approaches for finding if a string is inside an array of strings: using the `includes()` method and using the `indexOf()` method with a check for -1. **Includes() Method** * Description: The includes() method returns true if the specified value is found in the array, otherwise false. * Pros: + Shorter and more concise code + Less computationally expensive, as it only requires a single iteration over the array + More readable and easier to understand * Cons: + Not all browsers support the includes() method (only introduced in ECMAScript 2020) + May not be suitable for large arrays or performance-critical applications **IndexOf() Method with -1 Check** * Description: The indexOf() method returns the index of the first occurrence of the specified value, or -1 if it's not found. * Pros: + Widely supported across different browsers and versions + Suitable for both small and large arrays + Allows for additional logic to be executed if the value is not found (e.g., using a default value) * Cons: + Longer code requires more operations (two iterations over the array, one comparison, etc.) + May be less readable due to the additional checks **Library Used: None** The benchmark does not involve any external libraries. The JavaScript functions used are built-in and supported by most modern browsers. **Special JS Features/Syntax: None** There is no special JavaScript feature or syntax being tested in this benchmark. **Alternatives** Other alternatives for finding if a string is inside an array of strings include: * Using `some()` method: `arr.some(function(item) { return item === 'hello'; })` + Pros: concise and readable, works well with large arrays + Cons: not all browsers support the some() method (introduced in ECMAScript 2015) * Using a regular expression: `/hello/g.test(arr)` + Pros: flexible and powerful, suitable for both small and large arrays + Cons: may be less readable due to the use of regex, computationally expensive The benchmark results show that the `includes()` method is significantly faster than the `indexOf()` method with a -1 check on Firefox 91. However, it's essential to note that this result may vary depending on the specific browser, version, and hardware configuration being tested. As software engineers, understanding these differences can help you make informed decisions when writing performance-critical code or optimizing existing functions for better efficiency.
Related benchmarks:
elielielieli
Array empty
Array empty 2
check getting length of array
Comments
Confirm delete:
Do you really want to delete benchmark?