Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval inderect call vs new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
3 years 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
func();
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain what's being tested in this benchmark. **Benchmark Overview** The benchmark is comparing the performance of two approaches: using `new Function` and using `eval`. The goal is to determine which approach is faster for creating a simple JavaScript function that returns the result of an expression (in this case, `2 * 3`). **Approaches Compared** 1. **new Function**: This approach creates a new JavaScript function by stringifying the desired functionality into a string and then using the `Function` constructor to create a new function object. 2. **eval**: This approach uses the `eval` function to execute a string containing the desired expression. **Pros and Cons of Each Approach** 1. **new Function** * Pros: + Can be more secure than `eval`, as it allows for sandboxing and prevents code injection attacks. + Can be more predictable, as the execution order is controlled by the developer. * Cons: + May require more boilerplate code to set up and manage the function. + May not support dynamic or unknown function names. 2. **eval** * Pros: + Can create functions dynamically, making it useful for situations where the function name is not known until runtime. + Can be faster in some cases, as `eval` can execute expressions without creating a new function object. * Cons: + Can be less secure than `new Function`, as it allows code injection attacks and executes arbitrary code. + Can be slower due to the overhead of executing an expression. **Library Usage** There is no library being used in this benchmark. The tests only involve JavaScript core functionality. **Special JS Features/Syntax** None are mentioned. **Other Alternatives** If `eval` is not considered secure or desirable, other alternatives for dynamic function creation include: * **Function constructor**: Similar to `new Function`, but allows for more control over the function's behavior. * **Arrow functions**: A shorthand syntax for creating small anonymous functions that can be used as values in an expression. * **Function expressions**: A way of creating functions directly in the code, using a variable name or symbol. **Benchmark Result** The latest benchmark result shows that `new Function` outperforms `eval` in terms of executions per second. This suggests that for simple cases like this one, `new Function` might be a more efficient choice due to its lower overhead and ability to provide better control over the execution environment. However, it's essential to note that this result may vary depending on the specific use case and requirements.
Related benchmarks:
eval vs new Function v3
window.eval function vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?