Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.match vs Array.includes
(version: 0)
Comparing performance of:
RegEx.match vs Array.includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var appState = "active"; var regex = /inactive|background/; var arr = ['inactive', 'background'];
Tests:
RegEx.match
regex.match(appState);
Array.includes
arr.includes(appState);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.match
Array.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.match
0.0 Ops/sec
Array.includes
5589213.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition** The benchmark is defined in JSON format, specifying two test cases: 1. `Regex.match`: Tests the performance of the regular expression matching function (`regex.match`) on the string `"active"`. 2. `Array.includes`: Tests the performance of the array search function (`arr.includes`) on the same value `"active"`. **Script Preparation Code** The script preparation code is provided to set up the environment for the benchmark: ```javascript var appState = "active"; var regex = /inactive|background/; var arr = ["inactive", "background"]; ``` This code defines three variables: * `appState`: The string value to be tested in both benchmarks. * `regex`: A regular expression pattern that will be used for the `Regex.match` benchmark. This pattern matches either `"inactive"` or `"background"`. * `arr`: An array containing the same values as the `regex` pattern. **Benchmark Test Cases** The test cases are defined within an array: ```json [ { "Benchmark Definition": "regex.match(appState);", "Test Name": "RegEx.match" }, { "Benchmark Definition": "arr.includes(appState);", "Test Name": "Array.includes" } ] ``` Each test case has two parts: 1. `Benchmark Definition`: The actual code to be executed for each benchmark (e.g., `regex.match(appState)` or `arr.includes(appState)`). 2. `Test Name`: A human-readable name for the benchmark. **Library and Special JS Features** There are no external libraries used in this benchmark. However, note that this benchmark does not use any special JavaScript features beyond the regular expression syntax and array methods (`includes()`). **Pros and Cons of Different Approaches** The two approaches being compared here are: 1. **Regular Expression Matching (Regex.match)**: * Pros: Regular expressions can provide a flexible way to search for patterns in strings. * Cons: Regular expressions can be computationally expensive, especially for complex patterns. 2. **Array Search (Array.includes)**: * Pros: Array search is often faster and more efficient than regular expression matching, especially for simple searches. * Cons: It requires the value to be searched to be present in the array. **Other Considerations** When choosing between these two approaches, consider the following factors: * Complexity of the pattern: If you need to match a complex pattern with multiple conditions, regular expressions might be more suitable. However, for simple cases like this one, array search might be faster. * Data size and structure: If you're working with large datasets or arrays, array search might be more efficient. **Alternatives** If you need to compare other JavaScript methods, consider the following alternatives: 1. **String.prototype.indexOf()**: Similar to `Array.includes`, but part of the built-in String prototype. 2. **Regex.test()**: Another way to execute regular expressions in JavaScript. 3. **Other array methods**: Depending on your specific use case, you might want to test other array methods like `Array.isArray()` or `Array.prototype.indexOf()`. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the original benchmark.
Related benchmarks:
RegEx.test vs Array.includes
RegEx.test vs Array.includes (Lowercase)
String.match vs Array.includes
RegEx.test vs Array.includes -- 3 options
Comments
Confirm delete:
Do you really want to delete benchmark?