Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
window.eval function vs new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
eval
var func = window.eval("function () { return 2 * 3 }"); func();
new Function
var func = new Function("return 2 * 3"); func();
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
0.0 Ops/sec
new Function
2345037.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **What is being tested?** The provided JSON represents two JavaScript microbenchmarks that compare the performance of two approaches: 1. **`window.eval` function**: This method executes a string as JavaScript code, returning its result. In contrast to `new Function()`, it does not parse or execute the code in the context of an existing scope. 2. **`new Function()`**: This method creates a new JavaScript function object from a given string, allowing for execution and parsing in the context of an existing scope. **Options being compared:** The two approaches differ in how they handle: 1. **Execution context**: `window.eval()` executes the code without creating a separate scope, while `new Function()` creates a new function object with its own scope. 2. **Code parsing**: Both methods parse the input string into an Abstract Syntax Tree (AST), but `new Function()` also performs type checking and variable lookup. **Pros and cons of each approach:** 1. **`window.eval` function**: * Pros: faster execution time, as it avoids creating a new scope. * Cons: + Potential security risks if used with untrusted input, as it can execute arbitrary code in the global scope. + Limited support for variables and scoping. 2. **`new Function()`**: * Pros: + Safer execution, as it executes in a separate scope and avoids potential security risks. + Supports variables and scoping. * Cons: + Slower execution time due to the additional parsing and context creation. **Library usage:** None of the provided benchmarks use any external libraries. However, if you were to add code that uses a library like `ES6` or `ECMAScript`, it would be used for features like arrow functions, classes, or async/await syntax. **Special JS feature/syntax:** The provided benchmarks do not use any special JavaScript features or syntax beyond the standard ECMAScript syntax. **Other alternatives:** If you wanted to test other approaches, consider: 1. **`Function()` constructor**: Similar to `new Function()`, but may have performance advantages due to its optimized implementation. 2. **`eval()` with a scope object**: Instead of using the global scope, use an explicit scope object to control variable lookup and execution context. When creating your own benchmark, consider factors like: * Using a consistent test framework (e.g., `BenchmarkJS`) * Minimizing overhead from additional libraries or features * Carefully designing test cases to isolate the effects of each approach * Analyzing results with care, considering factors like environment, hardware, and interpreter variations.
Related benchmarks:
window.eval vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
eval vs Function() - variation 1
Comments
Confirm delete:
Do you really want to delete benchmark?