Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
window.eval function vs new Function2
(version: 0)
Comparing performance of:
eval vs new Function
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
eval
window.eval("function fn() { return 2 * 3 }")();
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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
0.0 Ops/sec
new Function
594158.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark with two test cases: 1. `window.eval function vs new Function2` 2. Two individual test cases: * `eval` * `new Function` **What is tested?** The first test case compares the performance of two approaches to execute a simple JavaScript expression: using the `eval()` method on the global `window` object versus creating a new function using the `new Function` constructor. The second set of test cases measures the execution speed of two different syntaxes for defining small functions: using the `eval()` method (`func = eval('function fn() { return 2 * 3; }');`) and the `new Function` constructor (`var func = new Function("return 2 * 3");;func();`). **Options compared** The two options being compared in the first test case are: 1. **`window.eval()`**: This method executes a string as JavaScript code, allowing you to define and execute small functions or expressions within a given scope. 2. **`new Function()`**: This constructor creates a new function object from a string or a list of arguments. **Pros and Cons** * **`window.eval()`**: + Pros: Easy to use, concise syntax. + Cons: Security risks if the input is not sanitized, can be slower due to JavaScript engine overhead. * **`new Function()`**: + Pros: More control over scope and environment, no security risks (if used correctly). + Cons: Longer syntax, requires more boilerplate code. **Library usage** In the individual test cases, `eval()` is used as a library function. `eval()` is a built-in JavaScript method that executes a string as JavaScript code. It can be powerful but also poses security risks if not used carefully. **Special JS feature/syntax** None mentioned in this specific benchmark definition. **Other alternatives** If you're interested in alternative approaches to executing small JavaScript functions or expressions, consider: 1. **`Function()`**: Similar to `new Function()`, but without the `new` keyword. 2. **`String.prototype.apply()`**: A more functional programming approach to execute a function from a string. 3. **`Closure libraries`**: Such as `es5-shim` or `closure-library`, which provide alternative ways to define and execute small functions. Keep in mind that these alternatives might introduce additional complexity, performance overhead, or security considerations. In conclusion, the benchmark on MeasureThat.net provides a simple yet informative comparison of two popular approaches to executing JavaScript expressions: `window.eval()` and `new Function()`. While each has its pros and cons, the choice ultimately depends on your specific use case, security requirements, and personal preference.
Related benchmarks:
window.eval vs new Function
window.eval function vs new Function
window.eval function vs new Function1
eval vs Function() - variation 1
Comments
Confirm delete:
Do you really want to delete benchmark?