Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs Reg StartsWith vs Reg Includes in string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs Reg StartWith vs Reg Includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'; var matcherSW = new RegExp("^tempor","i"); var matcherInc = new RegExp("tempor","i");
Tests:
IndexOf
string.indexOf('tempor')
Includes
string.includes('tempor')
Reg StartWith
matcherSW.test(string)
Reg Includes
matcherInc.test(string)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Reg StartWith
Reg 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 benchmark and explain what is being tested. **Overview** The benchmark compares four different ways to test if a string contains or starts with a certain substring: `indexOf`, `includes`, `Reg StartWith` (using regular expressions), and `Reg Includes` (also using regular expressions). **Options Compared** Here's a brief overview of each option: 1. **`indexOf(string, subString)`**: This method returns the index of the first occurrence of `subString` in `string`. If `subString` is not found, it returns -1. 2. **`includes(string, subString)`**: This method returns a boolean value indicating whether `subString` is present in `string`. 3. **`Reg StartWith` (using RegExp.test)**: This method tests if the string starts with the specified regular expression pattern. 4. **`Reg Includes` (using RegExp.test)**: Similar to `Reg StartWith`, but it returns true if the entire string matches the regular expression pattern. **Pros and Cons** Here are some pros and cons of each approach: 1. **`indexOf`**: * Pros: Fast, simple, and widely supported. * Cons: May return incorrect results for substrings that don't match exactly (e.g., "hello" vs. "hello world"). 2. **`includes`**: * Pros: Simple, fast, and easy to read. * Cons: May be slower than `indexOf` for very large strings. 3. **`Reg StartWith`**: * Pros: Fast, efficient, and supports more complex patterns (e.g., anagrams). * Cons: May require a good understanding of regular expressions. 4. **`Reg Includes`**: * Pros: Similar to `Reg StartWith`, but provides additional features like case-insensitivity and lookaheads. * Cons: Also requires knowledge of regular expressions. **Library Used** None, as this is a native JavaScript implementation. **Special JS Features or Syntax** None mentioned in the benchmark definition. However, note that some modern JavaScript versions (e.g., ECMAScript 2022) have additional features like `string.prototype.includes()`, which might be used if updated benchmarks were to include them. **Other Alternatives** Some alternative approaches for testing string matching could include: 1. **`String.prototype.match()`**: Similar to `Reg Includes`, but returns an array of matches instead of a boolean value. 2. **`String.prototype.search()`**: Similar to `indexOf`, but returns -1 if the substring is not found, unlike `indexOf`. 3. **Third-party libraries like RegEx or String matching libraries**, which might provide additional features and optimizations. Please note that this list is not exhaustive, and other approaches might be suitable depending on specific use cases and requirements.
Related benchmarks:
RegEx.test vs. String.includes vs. String.indexOf
includes + toLowerCase vs RegExp + i
indexOf vs regex vs RegExp regexp fork 3
Case insensitive RegEx.test vs. String.includes when string doesn’t match
Comments
Confirm delete:
Do you really want to delete benchmark?