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
llama3.1:latest
, generated one year ago):
Let's break down what's being tested and compared in this benchmark. **Benchmark Overview** The benchmark is comparing three ways to create a simple JavaScript function that returns the result of an expression `2 * 3`. The three approaches are: 1. Using `eval` with a string containing the function expression. 2. Creating a new function using the `new Function()` constructor. 3. Defining the function directly as an arrow function expression. **Test Cases** The individual test cases are executing each of these three functions and measuring their performance in terms of executions per second on a Chrome 99 browser running on Mac OS X 10.15.7. **Library or Feature Used** In this benchmark, there's no specific library being used, but rather built-in JavaScript features: * `eval` is a built-in function for evaluating string expressions. * The `new Function()` constructor is also a built-in feature for creating new functions from strings. * Arrow function expressions (`() => 2 * 3`) are a modern JavaScript syntax introduced in ECMAScript 2015 (ES6). **Options Compared** The three test cases compare the performance of: 1. `eval`: This approach involves executing a string expression using `eval`. While it's convenient, `eval` can be slower than direct execution due to its parsing overhead. 2. `new Function()`: This method creates a new function from a string expression and executes it directly. It's often used when the function body is generated dynamically or comes from an external source. 3. Predefined Function: This approach simply defines the function directly as an arrow function expression, which is executed immediately. **Pros and Cons** Here are some pros and cons of each approach: 1. `eval`: * Pros: Convenient for one-time execution of simple expressions. * Cons: Can be slower due to parsing overhead, and may expose security vulnerabilities if used with untrusted input. 2. `new Function()`: * Pros: Allows dynamic creation of functions from strings or other sources. * Cons: May incur additional parsing overhead compared to direct function definitions. 3. Predefined Function (arrow expression): * Pros: Direct execution, no overhead, and ideal for simple functions with a clear intent. * Cons: Limited flexibility when it comes to generating complex function bodies dynamically. **Other Considerations** When choosing between these approaches, consider the following: 1. **Security**: Avoid using `eval` if you're executing code from untrusted sources, as it can lead to security vulnerabilities. 2. **Performance**: For simple functions like this benchmark, predefined functions (arrow expressions) are likely to be faster due to direct execution and minimal overhead. 3. **Readability and Maintainability**: When writing JavaScript code, consider using predefined function definitions (arrow expressions) for simple functions as they provide better readability and maintainability. **Alternatives** If you need more complex or dynamic functionality generation, alternative approaches include: 1. Using a templating library or framework that can generate JavaScript code. 2. Implementing a custom parser or compiler to generate optimized JavaScript code. 3. Utilizing advanced JavaScript features like `class` definitions or `async/await` for more sophisticated use cases. By understanding the pros and cons of these approaches, you'll be better equipped to choose the most suitable method for your specific use case in your own projects!
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?