Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function v2
(version: 0)
Comparing performance of:
eval vs new Function
Created:
6 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 explain the benchmark and its results. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches: `new Function` and `eval`. The benchmark is defined by two scripts: 1. "Script Preparation Code": This code defines a function `func` using the `new Function` constructor, which takes a string argument `"return 2 * 3;"` as its body. When executed, this function will return the result of evaluating the expression `2 * 3`. 2. The first individual test case: "Benchmark Definition": This is a simple `eval` statement that evaluates the expression `2 * 3`. The benchmark aims to measure which approach is faster. **Options Compared** Two approaches are compared: 1. **new Function**: Using the `new Function` constructor to create a new function, and then executing it. 2. **eval**: Directly using the `eval` function to evaluate an expression. **Pros and Cons of Each Approach** * **new Function**: + Pros: More secure and efficient than `eval`, as it doesn't execute arbitrary code. + Cons: Requires creating a new function object, which can have overhead due to memory allocation and creation of the function's environment. * **eval**: + Pros: Very fast, as it can directly evaluate an expression without creating any intermediate objects or functions. + Cons: Insecure, as it can execute arbitrary code if the input is not sanitized. **Library and Purpose** In this benchmark, there is no explicit library used. However, `new Function` uses a constructor function internally to create the function object, which is a part of the JavaScript language specification. **Special JS Feature or Syntax** There are no special features or syntaxes mentioned in this benchmark. **Other Considerations** When evaluating the performance of these two approaches, it's essential to consider the following factors: * Security: In real-world applications, using `eval` can be a significant security risk if not properly sanitized. In contrast, `new Function` is generally more secure. * Performance: As mentioned earlier, both approaches have different overheads due to memory allocation and execution. **Alternatives** If you were to compare these two approaches in a real-world scenario, you might also consider other alternatives: 1. **Function expressions**: Instead of using the `new Function` constructor, you could use an anonymous function expression (e.g., `(function() { return 2 * 3; })()`). 2. **Arrow functions**: If available, arrow functions can be a more concise and efficient way to define small functions. 3. **Closures**: You could also explore using closures, where the result of one function is used as an argument to another function. Keep in mind that these alternatives may have different trade-offs regarding security, performance, and readability, depending on your specific use case.
Related benchmarks:
eval vs new Function #2
eval vs new Function (fix)
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?