Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Inline array vs regex
(version: 0)
Comparing performance of:
Regex instance and check vs Array instance and check
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var ButtonIconPosition = { Left: 'left', Right: 'right', }
Tests:
Regex instance and check
for (let i=0; i<1_000_000; i += 1) { const checker = new RegExp( `(${ButtonIconPosition.Left}|${ButtonIconPosition.Top})`, ) }
Array instance and check
for (let i=0; i<1_000_000; i += 1) { const checker = [ButtonIconPosition.Left, ButtonIconPosition.Right] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex instance and check
Array instance and check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0
Browser/OS:
Firefox 123 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex instance and check
8.5 Ops/sec
Array instance and check
1813.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares two approaches for checking values in an array: using a regular expression (regex) and using the array itself. **Options Compared** Two options are compared: 1. **Regex instance**: This approach uses a regex pattern to check if a value is present in the `ButtonIconPosition` object. The regex pattern matches either the "Left" or "Right" values. 2. **Array instance**: This approach simply checks if the value is present in the array `[ButtonIconPosition.Left, ButtonIconPosition.Right]`. **Pros and Cons** * **Regex instance**: + Pros: Can be more flexible and powerful for complex pattern matching. + Cons: May have slower performance due to the overhead of creating a regex object and executing the pattern. + Considerations: Regex can be brittle and prone to errors if not used carefully. Additionally, regex engines may not be optimized for JavaScript and can lead to performance issues. * **Array instance**: + Pros: Simple, fast, and easy to understand. + Cons: Limited flexibility compared to regex. + Considerations: This approach assumes that the values in the array are always present in the `ButtonIconPosition` object. If the order or value of the array changes, this approach may break. **Library** The `RegExp` class is used in the benchmark. It provides a way to define and execute regular expression patterns in JavaScript. **Special JS Feature/Syntax** None mentioned in this specific benchmark. **Other Alternatives** If you need more flexibility or performance, you could consider using: * **String.includes()**: A method that checks if a string includes a specified value. * **Array.indexOf()**: A method that returns the index of a value in an array, or -1 if not found. * **Regex alternatives**: Other regex engines like V8's `RegExp` engine or specialized libraries like `regex-js`. Note: The benchmark is designed to compare performance between these two approaches. If you need more control over the test or want to explore other optimizations, consider modifying the benchmark code or using a different testing framework.
Related benchmarks:
Array.prototype.slice Versus Spread Operator
Javascript Object vs Array.indexOf
Simple array spread vs array push
Move item in first place spread vs unshift
Move item in first place unshift vs spread
Comments
Confirm delete:
Do you really want to delete benchmark?