Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DTMF: array includes vs regex
(version: 0)
compares the performance between array includes vs regex
Comparing performance of:
Regex vs Array includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var dtmfTone = "9"; var regex = /^[0-9A-D#*]+$/; var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "#", "*"];
Tests:
Regex
regex.test(dtmfTone);
Array includes
arr.includes(dtmfTone);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
6816731.5 Ops/sec
Array includes
6791882.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, which compares the performance between using regular expressions (regex) and array `includes()` methods for a specific task: testing if a string contains a specific DTMF tone. **Test Case 1: Regex** In this test case, the benchmark definition is `regex.test(dtmfTone);`. This code uses the `test()` method of the regex object to check if the string `dtmfTone` matches the regular expression `/^[0-9A-D#*]+$/`. **Pros and Cons:** * **Regex:** + Pros: - Highly flexible and powerful for pattern matching. - Can be used to perform complex text analysis tasks. + Cons: - Can be computationally expensive due to the complexity of regex patterns. - May require more memory allocations and iterations, leading to slower performance. **Test Case 2: Array includes()** In this test case, the benchmark definition is `arr.includes(dtmfTone);`. This code uses the array `includes()` method to check if the string `dtmfTone` is present in the array `arr`. **Pros and Cons:** * **Array includes():** + Pros: - Generally faster than regex for simple string matching. - More straightforward and easier to read, as it's a native JavaScript method. + Cons: - May not be as flexible or powerful as regex for complex pattern matching. **Library:** In both test cases, the `regex` library is used. The `test()` method of the regex object is used to perform the match operation. **Special JS Feature/ Syntax:** There are no special JavaScript features or syntax used in these benchmark definitions. **Other Alternatives:** For this specific task, other alternatives could include: 1. **String.prototype.indexOf()**: Instead of using `arr.includes()`, you can use `arr.indexOf(dtmfTone)` to check for the presence of `dtmfTone` in the array. 2. **Array.prototype.findIndex()**: Similar to `indexOf()`, but returns the index of the first occurrence, rather than a boolean value. It's worth noting that these alternatives may not provide significant performance differences unless you're dealing with very large arrays or complex pattern matching scenarios. **Benchmark Preparation Code:** The provided script preparation code initializes variables for the DTMF tone and regex pattern. The `arr` array is created with various strings, including digits (0-9), letters (A-D), and special characters (#*).
Related benchmarks:
RegEx.test vs Array.includes 2
RegEx vs Array.includes
RegEx vs Array.includes v2
RegEx.test vs Array.includes — fork 1
Comments
Confirm delete:
Do you really want to delete benchmark?