Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs regex
(version: 2)
Using indexOf instead of regex to find a sub string
Comparing performance of:
indexOf vs No cached regex vs Cached regex vs includes
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var uri = 'klcdhhjkdbjkdjbjkb@published'; var uri2 = 'jecdkbbdbkjdbjdbkjdbkjdbkj'; var globalRegex = /@published/;
Tests:
indexOf
uri.indexOf('@published') !== 0; uri2.indexOf('@published') !== 0;
No cached regex
var validRegex = /@published/; validRegex.test(uri); validRegex.test(uri2);
Cached regex
globalRegex.test(uri); globalRegex.test(uri2);
includes
uri.includes('@published'); uri2.includes('@published');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
No cached regex
Cached regex
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
181930496.0 Ops/sec
No cached regex
19755024.0 Ops/sec
Cached regex
21320882.0 Ops/sec
includes
184304384.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** The test case compares the performance of different methods to find a substring within a string: `indexOf()`, regular expressions (regex), and the newer `includes()` method. **Test Cases:** 1. **`indexOf()`**: This tests using the `indexOf()` method, which returns the index of the first occurrence of a specified value. In this case, it's searching for `'@published'` in two different string variables (`uri` and `uri2`). 2. **No cached regex**: This test uses a regular expression (regex) to search for the substring `'@published'`. The regex is created on-the-fly each time the test runs. 3. **Cached regex**: Similar to the previous test, but it creates a global regex variable (`globalRegex`) and reuses it across multiple tests. 4. **`includes()`**: This tests using the newer `includes()` method, which returns `true` if the specified value is found within the string. **Options Compared:** * Using `indexOf()` vs regular expressions (regex) * Creating a global regex variable (`globalRegex`) to cache it for reuse across multiple tests **Pros and Cons of Different Approaches:** * **`indexOf()`**: + Pros: Simple, easy to use, and fast when the substring is small. + Cons: Inefficient when searching large strings or complex patterns. * **Regular Expressions (regex)**: + Pros: Powerful for matching complex patterns and can be reused across multiple tests by caching the regex variable. + Cons: More complex to write and interpret, may lead to performance issues if not optimized correctly. * **`includes()`**: + Pros: A newer method that's simpler and faster than `indexOf()`, while still providing basic substring matching capabilities. + Cons: May be less flexible or powerful compared to regex for certain use cases. **Library Usage:** No external libraries are used in this benchmark. The code relies solely on built-in JavaScript methods (`indexOf()`, regex, and `includes()`). **Special JS Feature or Syntax:** None mentioned in this test case. **Other Alternatives:** For complex substring matching tasks, you might consider using a dedicated library like `String.prototype.matchAll()` (introduced in modern browsers) or third-party libraries like `RegExp` or other string manipulation libraries. However, these alternatives are not being compared in this specific benchmark. I hope this explanation helps!
Related benchmarks:
(Draft) indexOf vs Regex
RegEx.test vs String.includes vs String.indexOf
RegEx.test vs. String.includes vs. String.indexOf
RegEx.test vs. String.includes vs. String.match vs. IndexOf
Comments
Confirm delete:
Do you really want to delete benchmark?