Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex v includes
(version: 0)
Comparing performance of:
includes vs regex
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var text = `{"A lot more stuff than what we have before and by a lot more i don't actually mean that much more but it's still more FetchInputEventCrashTimeout":"Watchdog FetchInputEvent were not being fed for 30000 milliseconds.Crash timeout was set to 30000 milliseconds."}`
Tests:
includes
const result = text.includes("Watchdog"); if (result){ console.log("something"); }
regex
const result = text.match(/\bWatchdog/); if (result){ console.log("something"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
112385.4 Ops/sec
regex
119300.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare two approaches: using `includes()` method with string interpolation and using regular expressions (regex) to search for a specific pattern in a string. **Script Preparation Code** The script preparation code defines a JavaScript object literal that contains a longer text: ```javascript var text = `{...` ``` This text is used as the input for both test cases. **Html Preparation Code** There is no HTML preparation code, which means that this benchmark only tests the JavaScript implementation and ignores any potential DOM-related factors. **Test Cases** There are two test cases: 1. **includes**: This test case uses the `includes()` method with string interpolation to search for the substring "Watchdog" in the input text. ```javascript const result = text.includes("Watchdog"); ``` 2. **regex**: This test case uses regular expressions (regex) to search for a specific pattern in the input text: ```javascript const result = text.match(/\\bWatchdog/); ``` **Pros and Cons of each approach** **includes() method:** Pros: * Easy to read and maintain * Fast and efficient, especially for simple searches * Native JavaScript implementation, so no additional dependencies Cons: * May be slower than regex for more complex patterns or large strings * Limited control over pattern matching and error handling **Regex approach:** Pros: * Provides fine-grained control over pattern matching and error handling * Can handle more complex patterns and larger strings efficiently * Can be used with various libraries and frameworks that provide regex support Cons: * More verbose and less readable than the `includes()` method * Requires additional dependencies and setup (e.g., JavaScript engine support) **Library: None** There is no library mentioned in the benchmark definition, but it's worth noting that some browsers may use internal regex engines or polyfills. **Special JS feature/syntax** None are explicitly used in this benchmark. However, it's essential to be aware of JavaScript features like: * Template literals (used for string interpolation) * String.prototype.includes() * RegExp objects If you were to extend or modify this benchmark, consider exploring these aspects to make your test cases more comprehensive. **Alternative alternatives** Other approaches to search for patterns in strings include: 1. **String.indexOf()**: Similar to `includes()` but returns the index of the first occurrence instead of a boolean value. 2. **String.prototype.indexOf()**: This method uses `indexOf()` internally and is similar to `string.indexOf()`. 3. **Regexp.test()**: A more flexible way to test strings against regex patterns, which can return a match object or null if no match is found. Keep in mind that these alternatives might have different performance characteristics or syntax requirements, so be sure to adjust your benchmark accordingly.
Related benchmarks:
includes v regex
regex vs compiled regex vs includes
RegEx.test vs RegEx.match when fails
regex.test v includes
Comments
Confirm delete:
Do you really want to delete benchmark?