Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function #2
(version: 0)
Comparing performance of:
eval vs new Function
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func_new = new Function("return 2 * 3"); var func_eval = eval("() => 2 * 3");
Tests:
eval
func_eval();
new Function
func_new();
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
149879840.0 Ops/sec
new Function
148111376.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark, where two different approaches are compared: `new Function` and `eval`. The script preparation code includes two functions, `func_new` and `func_eval`, which are defined using these two methods. The purpose of this benchmark is to measure the performance difference between creating and executing a function using these two methods. **Options Compared** The two options being compared are: 1. **new Function**: This method creates a new function by stringifying a code snippet and passing it to the `Function` constructor. The code snippet is executed immediately, and its result is stored in the newly created function. 2. **eval**: This method executes a string of JavaScript code as if it were part of the current scope. In this case, the code snippet is an arrow function that returns the product of two numbers. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **new Function**: + Pros: Can be used to create functions with arbitrary names and scoping. + Cons: Can be slower than `eval` due to the overhead of creating a new function object. * **eval**: + Pros: Fast and efficient, but can pose security risks if not used carefully (e.g., executing user-supplied code). + Cons: Limited control over function creation and scoping. **Library Usage** Neither of these methods relies on any specific library. However, `new Function` is a built-in JavaScript feature that has been available since the language's inception. **Special JS Feature/Syntax** The benchmark uses an arrow function (`() => 2 * 3`) which was introduced in ECMAScript 2015 (ES6). This syntax allows for concise and expressive code, but may not be supported by older browsers or environments. **Other Alternatives** If you wanted to create functions using other methods, you could consider: * Using a library like `func` or `bind` to create anonymous functions. * Using the `bind` method to create bound functions. * Using a different syntax for creating functions, such as using an immediately invoked function expression (IIFE). However, these alternatives may not be available on older browsers or environments. **Benchmark Results** The latest benchmark result shows that the `new Function` approach outperforms the `eval` approach. The Chrome Mobile 87 browser on a Android 7.0 device executed `new Function` approximately 5 times faster than `eval`, with an average of around 1,133,767 executions per second compared to 107,520 executions per second for `eval`.
Related benchmarks:
eval vs new Function (fix)
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?