Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs RegExp
(version: 1)
Comparing performance of:
eval vs RegExp
Created:
6 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let a = eval let b = RegExp
Tests:
eval
for(let i = 10; i--;) a('/hello|there/')
RegExp
for(let i = 10; i--;) b('/hello|there/')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
RegExp
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
2394946.0 Ops/sec
RegExp
1858018.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 6 months ago):
The benchmark defined in the provided JSON is focused on comparing the performance of two different approaches to executing a test case that evaluates a regular expression: using `eval` versus using the `RegExp` constructor in JavaScript. ### Benchmark Overview **Name:** `eval vs RegExp` **Description:** (none provided) ### Preparation Code - **Script Preparation Code:** - `let a = eval` - `let b = RegExp` Here, `a` is assigned the `eval` function, and `b` is assigned the RegExp constructor. ### Individual Test Cases 1. **Test Case using `eval`**: - **Benchmark Definition:** `for(let i = 10; i--;) a('/hello|there/')` - **Test Name:** `eval` - This tests how quickly the JavaScript `eval` function can execute the provided regular expression string multiple times. 2. **Test Case using `RegExp`**: - **Benchmark Definition:** `for(let i = 10; i--;) b('/hello|there/')` - **Test Name:** `RegExp` - This tests the performance of the `RegExp` constructor, which compiles the regular expression pattern into a regex object and executes it. ### Performance Results - For the benchmark run, the recorded results indicate: - The `eval` approach achieved **1986535.375 executions per second**. - The `RegExp` approach achieved **1586349.875 executions per second**. ### Pros/Cons and Other Considerations #### eval - **Pros:** - Highly flexible; can execute any string as JavaScript code. - Easier to dynamically generate and test code snippets on the fly. - **Cons:** - Performance is generally lower than dedicated methods (as evidenced in this benchmark). - Security risk: executing unsanitized input can lead to code injection vulnerabilities. - Usually, `eval` can hinder JavaScript engine optimizations, leading to slower execution compared to other methods. #### RegExp - **Pros:** - Designed specifically for matching patterns; generally more efficient than `eval` for regex operations. - Safer, as it does not involve executing arbitrary code. - Better suited for regex operations and is more consistent in performance. - **Cons:** - Less flexible than `eval`, as it only handles the creation of regex patterns and not arbitrary code execution. - Requires that your regex patterns be defined beforehand or constructed in simpler scenarios. ### Alternatives 1. **Using RegExp literals**: Instead of creating a RegExp object via the constructor, you can directly use regex literals, which are often more concise and potentially faster to execute. Example: `/hello|there/`. 2. **Other libraries or tools**: If you need regex features or performance beyond JavaScript's built-in functionality, libraries like XRegExp may offer extended features, but they may come with their own performance trade-offs and additional complexity. In summary, the benchmark examines a fundamental difference between executing JavaScript code (`eval`) and working within the specialized capabilities of the JavaScript engine regarding regex handling (`RegExp`). While the performance indicates `eval` is faster in this specific use case, it comes at the cost of security and potential maintainability issues, suggesting that for standard use cases, `RegExp` is usually the preferred approach.
Related benchmarks:
Demo benchmark
String compare VS regex
char index vs charAt() vs RegExp
Regex vs Forloop Performance
RegExp#test vs String#indexOf vs String#match vs pre-allocated RegExp
Test regexp vs startsWith
Demo benchmark fork - pre-initialized regex
starts/endswith vs regex.test
capturing and non capturing regex
Comments
Confirm delete:
Do you really want to delete benchmark?