Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs compiled regex vs includes
(version: 1)
Compares the difference in performance between regex, compiled regex and includes.
Comparing performance of:
includes vs regex vs compiled regex vs variable regex
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
// Note that var is used to take advantage of global scope 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."}` var regex = new RegExp('\bWatchdog'); var regexVar = /\bWatchdog/;
Tests:
includes
const result = text.includes("Watchdog"); if (result){ console.log("something"); }
regex
const result = text.match(/\bWatchdog/); if (result){ console.log("something"); }
compiled regex
// Note, the following is included in the preperation code // var regex = new RegExp('\bWatchdog'); const result = regex.test(text) if (result){ console.log("something"); }
variable regex
const result = regexVar.test(text) if (result){ console.log("something"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
regex
compiled regex
variable regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
290214.4 Ops/sec
regex
282872.1 Ops/sec
compiled regex
37707244.0 Ops/sec
variable regex
288676.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided benchmark compares the performance of three different approaches to search for a specific pattern in a large text: 1. **Includes**: The `includes()` method is used to check if a substring exists within another string. 2. **Compiled Regex**: A regular expression (regex) is created using the `RegExp` constructor and then used to search for the pattern in the text. 3. **Variable Regex**: The same regex pattern is defined as a variable (`regexVar`) and then used with the `test()` method. Now, let's discuss the pros and cons of each approach: **Includes** Pros: * Easy to use and understand * Built-in JavaScript function, so no additional dependencies required Cons: * Can be slower than other approaches due to the need to search through the entire string * May not be suitable for large strings or complex patterns **Compiled Regex** Pros: * Can be faster than `includes()` method, especially for large strings and complex patterns * Allows for more precise control over pattern matching and grouping Cons: * Requires creating a new regex object, which can add overhead * Can be more difficult to understand and debug, especially for complex patterns **Variable Regex** Pros: * Avoids the need to create a new regex object, reducing overhead * Can be faster than `includes()` method and compiled regex, due to caching effects in JavaScript engines Cons: * Requires defining the regex pattern as a variable, which can add complexity * May not be suitable for large strings or complex patterns, due to potential caching issues Other considerations: * The use of global scope variables (`var`) in the script preparation code allows the test to take advantage of global scope optimizations. * The `FetchInputEventCrashTimeout` and `Watchdog FetchInputEvent` constants are likely used for testing purposes only, such as simulating browser crashes or timeouts. Now, let's discuss some JavaScript features and libraries: * There is no explicit mention of any special JS feature or syntax in the benchmark. However, it's worth noting that JavaScript engines like SpiderMonkey (used by Firefox) have various optimizations and caching mechanisms that can affect performance. * The `RegExp` constructor is used to create a regex object, which is a built-in JavaScript function. * There is no explicit mention of any third-party libraries or dependencies in the benchmark. Finally, here are some alternative approaches that could be explored: * **String search algorithms**: Instead of using regex or built-in string methods, the test could compare different string search algorithms like Knuth-Morris-Pratt (KMP) or Boyer-Moore. * **Regular expression alternatives**: Other regular expression flavors like PCRE or Perl-compatible regex could be compared to JavaScript's built-in regex implementation. * **Just-In-Time (JIT) compilation**: The test could compare the performance of compiled code using JIT compilation techniques, which can lead to significant performance improvements. I hope this explanation helps you understand the benchmark and its underlying assumptions!
Related benchmarks:
RegEx.test vs String.includes vs String.indexOf
RegEx.test vs String.includes
RegEx.test (with inline regex) vs. String.includes vs. String.match
Long regex test vs string includes
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?