Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.includes vs string.indexOf vs regex.test(string) vs == vs ===
(version: 0)
Comparing performance of:
indexOf vs includes vs regex vs Equal vs Strict Equal
Created:
2 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.'
Tests:
indexOf
string.indexOf('ullamco')
includes
string.includes('ullamco')
regex
/ullamco/.test(string)
Equal
string == 'ullamco'
Strict Equal
string === 'ullamco'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
indexOf
includes
regex
Equal
Strict Equal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
380344416.0 Ops/sec
includes
11725476.0 Ops/sec
regex
424270048.0 Ops/sec
Equal
535005024.0 Ops/sec
Strict Equal
470092128.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is tested?** The provided JSON represents a benchmark that tests various string comparison methods in JavaScript: 1. `string.includes('ullamco')` 2. `string.indexOf('ullamco')` 3. `/ullamco/.test(string)` 4. `string == 'ullamco'` 5. `string === 'ullamco'` These tests aim to compare the performance of different methods for finding a specific substring or character within a string. **Options compared** The benchmark compares five different options: 1. **`string.includes('ullamco')`**: Uses the `includes()` method, which returns `true` if the specified value is found in the string. 2. **`string.indexOf('ullamco')`**: Uses the `indexOf()` method, which returns the index of the first occurrence of the specified value in the string. 3. **`/ullamco/.test(string)`**: Uses a regular expression (`/ullamco/`) and the `test()` method to match the substring against the entire string. 4. **`string == 'ullamco'`**: Uses the loose equality operator (`==`) to compare the strings. 5. **`string === 'ullamco'`**: Uses the strict equality operator (`===`) to compare the strings. **Pros and Cons of each approach** Here's a brief overview of the pros and cons of each approach: 1. **`includes()`**: * Pros: Fast, efficient, and widely supported. * Cons: May be slower for large strings due to the use of `indexOf()` internally. 2. **`indexOf()`**: * Pros: Can be used to find substrings starting from any position in the string. * Cons: Returns `-1` if not found, which can lead to errors if treated as a boolean value. 3. **`test()` with regex**: * Pros: Allows for precise matching using regular expressions. * Cons: Can be slower due to the overhead of compiling and executing the regex pattern. 4. **`==` (loose equality)**: * Pros: Simple and widely supported, but can lead to unexpected results due to type coercion. * Cons: Not recommended for precise comparisons. 5. **`===` (strict equality)**: * Pros: More accurate than `==`, but still has limitations due to type coercion. * Cons: May not work as expected with certain data types. **Library usage** The benchmark uses the `string` object, which is a built-in JavaScript object that represents a string. The `includes()`, `indexOf()`, and `test()` methods are all part of this object. **Special JS feature or syntax** There are no special features or syntax used in this benchmark beyond the standard JavaScript operators and methods. However, it's worth noting that some modern browsers support additional features like `String.prototype.includes()` (which is similar to `includes()`) and `String.prototype.indexOf()` (which can be slower than the built-in `indexOf()` method). **Alternatives** Other alternatives for string comparisons in JavaScript include: 1. **`Array.prototype.indexOf()`**: Can be used to find substrings within an array. 2. **`Regex.test()` with a flag**: Can be used to perform precise matching using regular expressions, with flags like `/g` or `/m` to enable global or multiline matching. 3. **`String.prototype.match()`**: Returns an array of matches if the pattern is found in the string. These alternatives may offer different trade-offs in terms of performance, accuracy, and readability, depending on the specific use case.
Related benchmarks:
string.includes vs string.indexOf vs regex.test(string)
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.test vs. String.includes incasesensitive
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?