Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function3
(version: 0)
Comparing performance of:
eval vs new Function 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.apply();
new Function()
new Function("return 2 * 3")();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
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):
**Overview of the Benchmark** The provided JSON represents a benchmarking test created on MeasureThat.net, comparing three approaches to evaluate a simple mathematical expression: `2 * 3`. The test consists of two parts: script preparation code and HTML preparation code. **Script Preparation Code** The first part is the script preparation code: ```javascript var func = new Function("return 2 * 3;"); ``` This code defines a function named `func` using the `new Function()` constructor, which takes an anonymous function as its argument. The anonymous function returns the result of evaluating `2 * 3`, which is then stored in the `func` variable. **Html Preparation Code** The second part is the HTML preparation code, which is empty in this case (`null`). This suggests that no specific HTML-related testing is being performed. **Individual Test Cases** There are three test cases defined: 1. **eval** ```javascript "eval(\"2 * 3\");" ``` This test case uses the built-in `eval()` function to evaluate the mathematical expression `2 * 3`. The result of this evaluation is not stored in a variable, as it is executed immediately. 2. **new Function** ```javascript "func.apply();" ``` This test case applies the function stored in `func` using the `apply()` method, effectively executing the original mathematical expression. 3. **new Function()** ```javascript "new Function(\"return 2 * 3\")();" ``` This test case creates a new instance of the `Function` constructor with an anonymous function that returns the result of evaluating `2 * 3`. The resulting function is then immediately invoked using the parentheses (`()`). **Library and Special JS Features** The only library used in this benchmark is the built-in `Function` constructor, which allows creating anonymous functions. There are no special JavaScript features or syntax explicitly mentioned. **Pros and Cons of Each Approach** 1. **eval** * Pros: Simple to implement, widely supported. * Cons: Can pose security risks if not properly sanitized, may have performance issues due to parsing the expression as JSON. 2. **new Function** * Pros: Allows for more control over function creation and execution. * Cons: May be slower than `eval` due to the overhead of creating a new function object. 3. **new Function()** * Pros: Provides a more explicit way of defining functions, can be useful in certain scenarios. * Cons: Similar to `new Function`, may have performance issues. **Other Considerations** When testing these approaches, it's essential to consider factors like: * Performance: How many executions per second can each approach handle? * Security: Are the results of `eval()` safe to use in production environments? * Control: Do you need fine-grained control over function creation and execution? **Alternatives** If you're looking for alternative approaches, consider using: 1. **Template literals**: A more modern way of evaluating expressions, which may offer better performance. 2. **Arrow functions**: A concise way to define small, single-purpose functions. 3. **Babel transpilation**: If you need to support older JavaScript versions, consider using Babel to transpile your code. Keep in mind that these alternatives might not be directly comparable to the original `eval()` vs `new Function` approach, but they can provide alternative solutions depending on your specific use case.
Related benchmarks:
eval vs new Function v3
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?