Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function - bool
(version: 1)
Comparing performance of:
eval vs func
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var func = new Function("return true || false && 10 != 7");
Tests:
eval
eval("true || false && 10 != 7");
func
func();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
func
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; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
2267291.8 Ops/sec
func
1046219392.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in question compares the performance of two different methods for executing JavaScript expressions: using `eval` and using the `Function` constructor to create a new function. ### Test Cases 1. **Eval Method**: - **Benchmark Definition**: `eval("true || false && 10 != 7");` - **Test Name**: "eval" 2. **Function Constructor Method**: - **Benchmark Definition**: `func();` (where `func` is defined as `new Function("return true || false && 10 != 7");`) - **Test Name**: "func" ### Comparison of Methods #### `eval` - **Pros**: - Flexibility: `eval` can execute any arbitrary JavaScript code in string form, which is useful in scenarios where code needs to be generated or modified at runtime. - Direct string execution: No need for any pre-defined function or scope. - **Cons**: - Performance: Generally slower due to the overhead of parsing the string every time it's executed. - Security risks: Using `eval` can lead to security vulnerabilities, especially if user input is involved, as it allows for the execution of potentially harmful code. - Debugging difficulty: Code passed to `eval` does not benefit from standard JavaScript debugging features. #### `Function` Constructor - **Pros**: - Performance: Usually faster than `eval` because a function is created once and can be executed multiple times without the overhead of parsing a string each time. - Scope: Functions created with the `Function` constructor execute in the global scope, which can be useful in specific scenarios. - Better encapsulation: Code inside the function does not pollute the global namespace as much as `eval` may. - **Cons**: - Limited flexibility: Can only create functions where the body is defined as a string, and cannot directly execute arbitrary code that does not conform to function syntax. - Still has security risks: While not as pronounced as `eval`, using dynamic code execution may still expose vulnerabilities if the input is not controlled. ### Performance Results From the latest benchmark results, we can see the performance metrics for each method: - **Function Constructor**: `Executions Per Second` is significantly higher (1,046,219,392.0) than `eval`, demonstrating decisive performance advantages. - **Eval**: The `Executions Per Second` rate is much lower at (2,267,291.75), highlighting its overall inefficiency compared to constructed functions. ### Conclusion In conclusion, while both `eval` and the `Function` constructor can execute dynamic code, the `Function` constructor generally offers better performance and safety, making it the preferred option in scenarios where dynamic code execution is needed. However, for cases where absolute flexibility is required, and security concerns are managed, `eval` may still be utilized with caution. Alternatives to these methods include more static programming practices or using safe alternatives like libraries designed to evaluate expressions in controlled environments (e.g., Sandboxes).
Related benchmarks:
eval vs new Function
eval vs new Function
eval vs new func
eval vs new Function v2
eval vs new Function 211
eval vs new Function fork
eval vs new Function (fix)
eval vs new Function1
eval vs new Function3
eval vs new Functionhhh
Comments
Confirm delete:
Do you really want to delete benchmark?