Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Exact string matching: RegExp vs Set
(version: 0)
Comparing performance of:
RegExp vs Set
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var count = 100000; var isoCodes = [ 'AUD', 'CAD', 'USD', 'GBP', 'EUR', 'SGD' ]; var isoCodesPattern = new RegExp('^\\b(' + isoCodes.join('|') + ')\\b$'); var isoCodeSet = new Set(isoCodes); var isoCodesToTest = isoCodes.concat( 'JPY', 'HKD', 'NZD' ).sort();
Tests:
RegExp
for (let i = 0; i < count; i++) { for (const isoCode of isoCodesToTest) { isoCodesPattern.test(isoCode); } }
Set
for (let i = 0; i < count; i++) { for (const isoCode of isoCodesToTest) { isoCodeSet.has(isoCode); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegExp
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
26 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegExp
41.9 Ops/sec
Set
153.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for exact string matching: using a regular expression (RegExp) versus a Set data structure in JavaScript. **What is being compared?** In this benchmark, we have three main options: 1. **RegExp**: This approach uses the `test()` method of a RegExp object to match strings against a pattern. 2. **Set**: This approach uses the `has()` method of a Set data structure to check if an element exists in the set. **Pros and Cons** **RegExp:** Pros: * Easy to implement and understand * Works well for simple string matching tasks Cons: * Can be slow due to the overhead of creating a RegExp object * May lead to slower performance if not optimized properly (e.g., using anchors or flags) **Set:** Pros: * Generally faster than RegExp for large datasets * Allows for efficient lookup and insertion of elements Cons: * Requires more memory to store the set, especially for large datasets * Can be less intuitive to understand and implement compared to RegExp **Library/Function used** In this benchmark, we are using the `RegExp` constructor to create a RegExp object with a pattern. The pattern uses anchors (`^` and `$`) to ensure exact matching. **Special JS feature/syntax** There is no special JavaScript feature or syntax being tested in this benchmark. Both approaches rely on standard JavaScript constructs. **Other alternatives** If you want to explore other options, here are a few alternatives: * **Array.prototype.includes()**: This method can be used as an alternative to `has()` for checking if an element exists in a set-like object. * **JSON objects**: You could also use JSON objects with property names (e.g., `{ "AUD": true }`) to achieve similar performance characteristics to the Set approach. **Benchmark preparation code** The script preparation code sets up the variables and constants used in the benchmark, including: * `count`: The number of iterations for each test case * `isoCodes`: An array of ISO codes (e.g., "AUD", "CAD") * `isoCodeSet`: A Set data structure containing the ISO codes * `isoCodesToTest`: An array of additional ISO codes to test beyond the original list **Individual test cases** Each test case is defined using a separate benchmark definition, which includes: * The `Benchmark Definition` string: This specifies the code that will be executed during the benchmark * The `Test Name`: A descriptive name for the test case (e.g., "RegExp" or "Set") The benchmark execution results are stored in the `RawUAString`, `Browser`, `DevicePlatform`, `OperatingSystem`, and `ExecutionsPerSecond` fields, providing insight into the performance characteristics of each approach.
Related benchmarks:
Alphanumeric String
정규식 vs includes
Alphanumeric String test 2
regex vs js - not Alphanumeric String
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?