Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
asdasdasdasdasdasdasda
(version: 1)
Comparing performance of:
eval vs new Function
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("2 * 3");
new Function
new Function("return 2 * 3")();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
new Function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
2276805.0 Ops/sec
new Function
871531.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of two JavaScript code execution methods: `eval` and `new Function()`. ### Tested Options 1. **`eval("2 * 3")`:** - This method evaluates a string of JavaScript code at runtime. In this case, it computes the expression `2 * 3`. - **Pros:** - It allows for dynamic code execution from strings, which can be powerful for scenarios where code to be executed is determined at runtime. - **Cons:** - It poses security risks, especially when evaluating strings derived from user input, as it can lead to code injection attacks. - Generally slower than alternatives because the engine has to parse and execute the code dynamically. 2. **`new Function("return 2 * 3")()`:** - This method defines a new function from a string of JavaScript code and immediately invokes it. The string passed to `new Function` is treated as the body of the function, and `return` is used to compute the value. - **Pros:** - Generally considered safer than `eval` since the function's context is not the local scope, reducing the risk of accidental or malicious access to local variables. - It can be faster than `eval`, as the engine can optimize function calls better. - **Cons:** - Similar to `eval`, it can execute string-based code, which has its own security concerns if used improperly. - It may still have some overhead due to the creation of a new function object. ### Benchmark Results The benchmark results show significant differences in performance as measured by "Executions Per Second": - **`eval`:** 2,276,805 executions per second - **`new Function`:** 871,531.75 executions per second Here, `eval` outperforms `new Function` by a substantial margin in this specific test case. This result is somewhat surprising since in many contexts, `new Function` can yield better performance due to its context safety and optimization capabilities. However, the difference in this particular case may arise due to how the JavaScript engine optimizes each method, along with the simplicity of the benchmarked expressions. ### Considerations and Alternatives - While both methods allow for dynamic code execution in JavaScript, for most everyday programming tasks, it's advisable to avoid both `eval` and `new Function` due to their performance implications and security risks. Instead, consider using: - **Direct functions:** Use regular functions or closures whenever possible for defined logic. - **Object methods:** Maintain a map of pre-defined functions that can be called based on input, rather than generating code dynamically. - **Web Workers:** For CPU-intensive tasks, consider using Web Workers for parallel execution. In summary, while this benchmark provides insights into the relative performance of `eval` and `new Function` in a controlled scenario, engineers should typically seek safer, more efficient alternatives for dynamic code execution in JavaScript programming.
Related benchmarks:
eval vs new Function
eval vs new func
eval vs new Function v2
test234
eval vs new Function1
eval vs new Function vs defined function
eval vs new Function vs function 1337
eval vs new Function3
testdfghdrfh
eval func
Comments
Confirm delete:
Do you really want to delete benchmark?