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:
5 years ago
by:
Guest
Jump to the latest result
Tests:
eval
eval("2 * 3");
new Function
Function("return 2 * 3")();
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
36053252.0 Ops/sec
new Function
14050206.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested on the provided JSON?** The benchmark tests two different approaches to perform simple arithmetic operations in JavaScript: 1. `eval()`: The `eval()` function evaluates a string as a JavaScript expression and returns its result. In this case, it's used to calculate `2 * 3`. 2. `new Function()`: The `Function()` constructor creates a new JavaScript function and executes it when called. **Options compared** The benchmark compares the performance of these two approaches: * `eval()` * `new Function()` **Pros and Cons of each approach:** * **`eval()`**: + Pros: - Simple to implement, as it's a built-in JavaScript function. - Can be used with any string expression. + Cons: - Can pose security risks if the input is not sanitized, as it can execute arbitrary code. - May be slower than other approaches due to its dynamic compilation step. * **`new Function()`**: + Pros: - More secure than `eval()`, as it compiles the expression into an anonymous function before execution. - Can provide better performance, especially for complex expressions, since the compilation step is done ahead of time. + Cons: - Requires more code to implement, which can be a disadvantage in terms of readability and maintainability. **Library** There is no external library used in this benchmark. Both `eval()` and `new Function()` are built-in JavaScript functions. **Special JS feature or syntax** None mentioned, but it's worth noting that the use of `eval()` and `Function()` can be considered a legacy approach in modern JavaScript development, as newer approaches like arrow functions (`() =>`) or template literals (`${expression}`) are generally recommended for simple expressions. **Other alternatives** If you're interested in exploring alternative approaches, here are some options: * **Arrow functions**: `(x) => x * 3` (similar to `new Function()` but with improved syntax) * **Template literals**: `x * 3` * **Native arithmetic operators**: `2 * 3` These alternatives may offer better performance or security benefits compared to the original `eval()` and `Function()` approaches. However, they may require changes to your code's implementation. Please let me know if you have any further questions!
Related benchmarks:
eval vs new Function
eval vs new Function #2
eval vs new Function (fix)
window.eval function vs new Function
Comments
Confirm delete:
Do you really want to delete benchmark?