Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs Array.includes bench
(version: 0)
Comparing performance of:
RegEx.test vs Array.includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var prop = "left-start"; var regex = /-(start|end)^/; var arr = ['start', 'end'];
Tests:
RegEx.test
regex.test(prop);
Array.includes
arr.find(q => prop.includes(`-${q}`));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
Array.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
40171844.0 Ops/sec
Array.includes
14654302.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Description:** The benchmark compares two approaches to test if a string contains a specific pattern: 1. **Regex.test**: Using the `test()` method of a regular expression object (`regex`) to check if the input string matches the pattern. 2. **Array.includes**: Using the `includes()` method of an array to search for a substring that matches the pattern. **Options Compared:** * **Regex.test vs Array.includes** + Both methods test if the input string contains the pattern "-(start|end)". + The main difference is in how the pattern is represented: - Regex.test uses a regular expression object to represent the pattern. - Array.includes uses an array of substrings that match the pattern. **Pros and Cons:** * **Regex.test** + Pros: - More concise and readable way to represent patterns using regular expressions. - Can be faster for complex patterns or when used with other JavaScript features (e.g., `regex.test()` is often implemented in native code). + Cons: - May be slower than Array.includes for simple patterns due to the overhead of creating a regular expression object. * **Array.includes** + Pros: - Can be faster for simple patterns because it avoids the overhead of creating a regular expression object. - More intuitive for some developers who are familiar with array methods. + Cons: - Less concise and readable way to represent patterns compared to Regex.test. - May require more setup or customization to work correctly. **Library Used:** In this benchmark, no external libraries are used. The `Array.includes()` method is a built-in JavaScript method that searches for a specified value within an array. **Special JS Feature/ Syntax:** None of the test cases use special JavaScript features or syntax beyond what's standard in modern JavaScript implementations. **Benchmark Preparation Code:** The preparation code sets up two variables: * `prop`: a string containing the pattern to search for ("left-start"). * `regex`: a regular expression object representing the same pattern. * `arr`: an array of strings that contains substrings matching the pattern ("start" and "end"). **Other Alternatives:** For similar benchmarks, you might consider testing: 1. **String.indexOf()**: Another method to search for a pattern within a string. 2. **RegExp.prototype.test() with a different type of RegExp**: Testing with different types of regular expressions (e.g., `/[-a-zA-Z]/` instead of `/-[s]*/`) could provide insights into how the engine handles different pattern syntax. Keep in mind that these alternatives might change the comparison, so it's essential to tailor your benchmarking approach to the specific question being asked.
Related benchmarks:
RegEx.test vs String.startsWith
array.some vs regex.test
RegEx vs Array.includes v2
RegEx.test vs Array.includes — fork 1
Comments
Confirm delete:
Do you really want to delete benchmark?