Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
window.eval function vs new Function1
(version: 0)
Comparing performance of:
eval vs new Function
Created:
4 years ago
by:
Guest
Go to the latest result
Tests:
eval
var func = window.eval("function fn() { 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 (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
new Function
628K/s
eval
0/s
View exact numbers
Test name
Executions per second
✓
new Function
627,730 Ops/sec
eval
0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark is comparing two approaches to create an anonymous function in JavaScript: `window.eval` and `new Function`. The goal is to determine which approach is faster, as measured by the number of executions per second. **Options compared** Two options are being compared: 1. **`window.eval`**: This method creates a new function by executing a string as JavaScript code. It's often used for client-side scripting and is available in all modern browsers. 2. **`new Function`**: This method creates a new function by passing a string of JavaScript code to the `Function` constructor. It's also widely supported in modern browsers. **Pros and Cons** Here are some pros and cons of each approach: * **`window.eval`**: * Pros: * Generally faster than `new Function`, as it avoids the overhead of creating a new function object. * Simplifies code, as you don't need to worry about function syntax or quotes. * Cons: * Can be security risk if used with untrusted input, as it can execute arbitrary JavaScript code. * May not work in all environments (e.g., older browsers, Node.js). * **`new Function`**: * Pros: * Provides better control over the function's syntax and behavior. * Can be used with untrusted input, making it more secure than `window.eval`. * Cons: * Generally slower than `window.eval`, due to the overhead of creating a new function object. **Library/dependency usage** There is no library or dependency being used in this benchmark. The code is pure JavaScript. **Special JS features/syntax** There are no special JavaScript features or syntax being used in this benchmark. It's focused on comparing two basic approaches to create an anonymous function. **Alternative approaches** If you wanted to test other approaches, some alternatives could include: * **`Function.prototype`**: This method creates a new function by passing a string of JavaScript code to the `Function.prototype` constructor. * **`eval` with a `with` clause**: This approach uses `eval` with a `with` clause to create an anonymous function. However, this is generally slower and less secure than `window.eval`. To test these alternatives, you would need to modify the benchmark's code and update the `Benchmark Definition` json accordingly. Here is how the updated Benchmark Definition might look like: ```json { "Name": "eval vs new Function", "Description": null, "Script Preparation Code": null, "Html Preparation Code": null } ``` And here is an example of a test case for `Function.prototype` and alternative approach using `eval` with a `with` clause: ```json { "Benchmark Definition": "var func = new Function(\"return 2 * 3\");\r\nfunc();", "Test Name": "new Function" }, { "Benchmark Definition": "with ({})) { var func = eval(\"function fn() { return 2 * 3 }\"); func(); }", "Test Name": "eval with with clause" } ``` You can add more test cases to compare other approaches, depending on your specific needs and goals.
Related benchmarks
window.eval function vs new Function
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?