Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs indexOf (Array and String)
(version: 2)
Comparing performance of:
string.indexOf vs string.includes vs array.indexOf vs array.includes
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
let array = ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipisicing", "elit.", "Perferendis", "rerum", "quos,", "laudantium", "modi", "magnam", "inventore", "ab", "deleniti", "vero", "eos", "aperiam", "facilis", "laboriosam", "sint!", "Sed", "eius", "facere", "tenetur", "nostrum.", "Doloribus,", "itaque."];
Tests:
string.indexOf
let string = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Perferendis rerum quos, laudantium modi magnam inventore ab deleniti vero eos aperiam facilis laboriosam sint! Sed eius facere tenetur nostrum. Doloribus, itaque."; string.indexOf("magnam") string.indexOf("adipisicing") string.indexOf("not found")
string.includes
let string = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Perferendis rerum quos, laudantium modi magnam inventore ab deleniti vero eos aperiam facilis laboriosam sint! Sed eius facere tenetur nostrum. Doloribus, itaque."; string.includes("magnam") string.includes("adipisicing") string.includes("not found")
array.indexOf
let array = ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipisicing", "elit.", "Perferendis", "rerum", "quos,", "laudantium", "modi", "magnam", "inventore", "ab", "deleniti", "vero", "eos", "aperiam", "facilis", "laboriosam", "sint!", "Sed", "eius", "facere", "tenetur", "nostrum.", "Doloribus,", "itaque."]; array.indexOf("magnam") array.indexOf("adipisicing") array.indexOf("not found")
array.includes
let array = ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipisicing", "elit.", "Perferendis", "rerum", "quos,", "laudantium", "modi", "magnam", "inventore", "ab", "deleniti", "vero", "eos", "aperiam", "facilis", "laboriosam", "sint!", "Sed", "eius", "facere", "tenetur", "nostrum.", "Doloribus,", "itaque."]; array.includes("magnam") array.includes("adipisicing") array.includes("not found")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
string.indexOf
string.includes
array.indexOf
array.includes
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 the provided benchmark and explain what is being tested, compared options, pros and cons of those approaches, and other considerations. **Benchmark Overview** MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare performance of different approaches. The current benchmark measures the performance of three methods: `indexOf`, `includes`, and their corresponding array versions (`array.indexOf` and `array.includes`) on both strings and arrays. **Tested Options** 1. **String `indexOf`**: This method is used to search for a substring within a string. 2. **String `includes`**: This method is used to check if a string contains a specific value or pattern. 3. **Array `indexOf`**: Similar to the string version, but searches within an array of values instead. 4. **Array `includes`**: Similar to the string version, but checks if an array includes a specific value or pattern. **Comparison** The benchmark compares the performance of each method across different scenarios: * String `indexOf`: Finds "magnam" and "adipisicing" in the provided string. * String `includes`: Checks for "magnam", "adipisicing", and "not found". * Array `indexOf`: Finds "magnam" and "adipisicing" in the array of strings. * Array `includes`: Checks for "magnam", "adipisicing", and "not found". **Pros and Cons** Here are some pros and cons of each approach: 1. **String `indexOf`**: Pros: Efficient, fast, and widely supported. Cons: May be slow for large strings or when searching for an exact match. 2. **String `includes`**: Pros: More flexible than `indexOf`, handles partial matches, and is more forgiving. Cons: Slower than `indexOf`. 3. **Array `indexOf`**: Similar pros and cons as string `indexOf`. 4. **Array `includes`**: Similar pros and cons as string `includes`. **Other Considerations** When choosing between these methods, consider the following: * Use `indexOf` for exact matches, especially when searching within a large dataset. * Use `includes` for partial matches or when you need to handle cases with typos or variations in spelling. * Use `array.indexOf` and `array.includes` when working with arrays of values. **Alternatives** Some alternative methods that may be worth considering include: 1. **Regular Expressions**: Can provide more flexibility, but can also be slower due to the overhead of parsing and compiling regular expressions. 2. **String matching libraries**: Such as `String.prototype.match()` or third-party libraries like `sregex` or `regex.js`, which offer more features than basic `indexOf` and `includes`. 3. **Custom solutions**: Depending on your specific use case, you might be able to optimize your code further by using custom methods that take advantage of the data structure or algorithm being used. Overall, these benchmarks provide a good starting point for understanding the performance differences between different JavaScript methods, but may not cover every edge case or optimization opportunity.
Related benchmarks:
IndexOf vs Includes vs lodash includes on the arrays of strings
String in Array - includes() vs indexOf() vs test()
Array of strings, indexOf vs includes 2
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?