Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Eval vs new Function (when both return a function)
(version: 3)
Comparing performance of:
eval vs new Func vs predefined Func
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var evalFunc = eval('(function(){ return () => 2 * 3; })();'); var newFunc = new Function('return 2 * 3'); var predefinedFunc = () => 2 * 3;
Tests:
eval
evalFunc();
new Func
newFunc();
predefined Func
predefinedFunc();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Func
predefined Func
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
gemma2:9b
, generated one year ago):
This benchmark compares the performance of three different ways to create a function in JavaScript: * **`eval()`:** Executes a string as JavaScript code. In this case, it executes a string that defines an anonymous function returning the result of 2 * 3. * **`new Function()`:** Constructs a new function object from a string. Similar to `eval()`, it's given a string defining a function returning 2 * 3. * **Predefined Function:** This approach simply defines the function directly using arrow function syntax: `() => 2 * 3`. **Pros and Cons:** * **`eval()`**: * **Pro:** Can be convenient for dynamically creating functions from user input or other sources. * **Con:** Significant performance overhead compared to direct function definition. It's also a security risk if the string passed to `eval()` comes from untrusted sources, as it can execute arbitrary code. * **`new Function()`**: * **Pro:** Similar to `eval()`, allows for dynamic function creation. * **Con:** Also suffers from performance issues and carries similar security risks as `eval()`. It's generally considered less performant than `eval()` in most cases. * **Predefined Function:** * **Pro:** Most performant option due to direct definition. No runtime overhead associated with parsing or executing code strings. * **Con:** Requires the function to be defined beforehand, limiting its flexibility compared to `eval()` and `new Function()`. **Alternatives:** * **Template Literals (for string interpolation):** If you need to construct strings dynamically, template literals are a safer and more efficient alternative to `eval()`. * **Function Constructors:** While not used in this benchmark, function constructors provide another way to create functions dynamically. They can sometimes be slightly faster than `new Function()` but are still generally less performant than direct function definition. **Key Takeaway:** When performance is crucial, avoid using `eval()` and `new Function()`. Directly defining your functions (using arrow functions or regular function declarations) is the most efficient approach in almost all cases.
Related benchmarks:
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?