Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
eval
var func = eval("(function (){ alert('sweet!')})");
new Function
var func = new Function("return function (){ alert('sweet!')}")();
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:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 26_2_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/142.0.7444.148 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 142 on iOS 26.2.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
34388608.0 Ops/sec
new Function
13557760.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its options. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark is defined as `eval vs new Function`, which means it compares the performance of two different approaches: using the `eval()` function and creating a new function with the `new Function` syntax. **Options Compared** There are two options being compared: 1. **`eval()`**: This function executes a string as JavaScript code, allowing developers to dynamically create and execute functions. 2. **`new Function`**: This syntax creates a new function object and executes it immediately. **Pros and Cons of Each Approach** * `eval()`: Pros: * Dynamically creates functions with variable names or without name. * Can be used for complex calculations or string manipulation. Cons: * Performance: Evaluating `eval()` can be slower due to the overhead of parsing the string as JavaScript code. * Security: Using `eval()` can pose security risks if used with untrusted input, as it can execute arbitrary code. * `new Function`: Pros: * Faster execution: Creating a new function using `new Function` is generally faster than using `eval()`. * Safer: It avoids the security risks associated with `eval()` when working with untrusted input. Cons: * Limited flexibility: The syntax requires specifying the return type of the function, which can limit its usability. **Library and Purpose** There are no libraries mentioned in this benchmark. However, it's worth noting that libraries like jQuery or Lodash might be used to create complex functions or manipulate strings, potentially affecting performance. **Special JS Features/Syntax** There is one special feature used in this benchmark: * **Immediately Invoked Function Expression (IIFE)**: Both benchmarks use an IIFE (`() => { ... }`) to create the function. This syntax ensures that the code within the function executes immediately, without relying on external function calls. **Other Alternatives** If you're interested in exploring alternative approaches for creating functions, consider the following: * **Function Expressions**: You can use a simple function expression (`function(){...}`) to create a new function. * **Arrow Functions**: Arrow functions (`() => { ... }`) are concise and often preferred for small, single-expression functions. Here is an example of how you could rewrite the benchmark using these alternatives: ```javascript // Using a function expression: var func = (function(){ return 'Hello, World!'; })(); console.log(func); // Output: "Hello, World!" // Using an arrow function: var func = () => 'Hello, World!'; console.log(func()); // Output: "Hello, World!" ``` Keep in mind that these alternatives might not be as efficient or safe as the original `new Function` syntax.
Related benchmarks:
eval vs new Function #2
eval() vs new Function()
eval vs new Function (fix)
eval vs evalFunction vs function
Comments
Confirm delete:
Do you really want to delete benchmark?