Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function (fix)
(version: 0)
Comparing performance of:
eval vs new Function
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("(function(){ return 2 * 3})();");
new Function
(new 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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
13889570.0 Ops/sec
new Function
3769704.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark. **What is being tested?** The provided JSON represents two test cases that compare the performance of `eval` versus `new Function` in creating a simple arithmetic expression. The benchmarks are designed to measure which approach is faster, more efficient, or has better performance characteristics. **Options compared:** Two options are being compared: 1. **`eval`**: A built-in JavaScript function that parses and executes a string as JavaScript code. 2. **`new Function`**: A constructor function used to create a new function object with a given string of JavaScript code. **Pros and Cons of each approach:** * `eval`: + Pros: - Easy to use, as it's a built-in function that can be called directly. - Can evaluate arbitrary JavaScript expressions. + Cons: - Performance overhead due to parsing and executing the string as code. - May introduce security vulnerabilities if not used carefully (e.g., evaluating user-input data). * `new Function`: + Pros: - More flexible than `eval`, as it allows creating a new function with specific parameters. - Can be used to create functions with a more structured syntax. + Cons: - May have additional overhead due to the creation of a new function object. **Library and purpose:** There is no library being used in this benchmark. The `new Function` approach is a built-in JavaScript feature that's part of the language. **Special JS features or syntax:** None are mentioned explicitly, but `eval` can be affected by some special cases, such as: * **Closures**: When using `eval`, it can create closures with the surrounding scope, which may have unexpected effects. * **Scope chains**: `new Function` can create a new scope chain for the created function, which might be desirable or undesirable depending on the use case. **Other alternatives:** If you're looking for alternative approaches to creating simple arithmetic expressions in JavaScript, some options include: * Using template literals (`let result = 2 * 3;`) * Using a library like `mathjs` or `numjs` for numerical computations. * Using a function expression with a single line of code (e.g., `function add(x, y) { return x + y; }()`) Keep in mind that these alternatives might not be as straightforward as using `eval` or `new Function`, but they can offer more control over the computation process and potentially better performance. I hope this explanation helps software engineers understand what's being tested in this JavaScript microbenchmark!
Related benchmarks:
eval vs new Function #2
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?