Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Long regex test vs string includes
(version: 0)
Comparing performance of:
regex vs includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "HelaasdfgafsdfsfdsadfsadfsadsadgfdgaesssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssHello world!"; var regex = /Hello/;
Tests:
regex
regex.test(string);
includes
string.includes("Hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
13 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
20045258.0 Ops/sec
includes
102263080.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark definition is represented by two JSON objects: `Script Preparation Code` and `Html Preparation Code`. These codes are executed before running the actual benchmark test. In this case, we have: * `Script Preparation Code`: This code defines a string variable `string` containing the text "Hello world!" and a regular expression variable `regex` with the pattern "/Hello/". * `Html Preparation Code`: This field is empty, indicating that no HTML preparation is needed for this benchmark. **Options being compared** The two test cases being compared are: 1. `regex.test(string);` 2. `string.includes("Hello");` These two tests are designed to measure the performance of JavaScript's built-in regular expression engine versus a simple string inclusion method using the `includes()` method. **Pros and Cons** Here's a brief analysis of each approach: * **Regular Expression Engine (`regex.test(string);`)**: + Pros: Regular expressions offer a flexible way to search for patterns in strings. This implementation may be suitable when dealing with complex pattern matching. + Cons: Regular expression engines can be slower than simple string operations due to the overhead of parsing and executing the regex pattern. * **Simple String Inclusion (`string.includes("Hello");`)**: + Pros: String inclusion is a simple, lightweight operation that typically has low overhead. This approach may be suitable when dealing with small strings or short searches. + Cons: The `includes()` method may not work as expected if the search string is not found at the beginning of the string. **Library and syntax** There are no notable libraries being used in this benchmark, but we can observe that the regular expression engine is using a specific syntax (`/Hello/`). As for special JavaScript features or syntax, there's no explicit mention of any advanced features like async/await, Promises, or ES6+ syntax. **Other alternatives** If you're looking to optimize string inclusion operations, some alternative approaches could include: * Using the `indexOf()` method instead of `includes()`, as it may be faster in some cases. * Using a library like Lodash's `includes` function, which is optimized for performance. * Implementing a custom string search algorithm using bitwise operations or other low-level techniques. Keep in mind that these alternatives would require additional code and testing to confirm their performance benefits. I hope this explanation helps you understand the JavaScript microbenchmark being tested on MeasureThat.net!
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs String.includes
Longer regex test vs string includes
RegEx.test vs. String.includes 3
Comments
Confirm delete:
Do you really want to delete benchmark?