Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function strict
(version: 0)
Comparing performance of:
eval vs new Function vs new Function strict
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func1 = new Function('return 2 * 3'); var func2 = new Function('"use strict";return 2 * 3');
Tests:
eval
eval("2 * 3");
new Function
func1();
new Function strict
func2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
new Function strict
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
7082180.0 Ops/sec
new Function
122356592.0 Ops/sec
new Function strict
126589512.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. **Benchmark Overview** The benchmark is comparing three different approaches: 1. `eval` (without quotes): This approach uses the built-in JavaScript function `eval()` to execute a string as code. 2. `new Function("return 2 * 3")`: This approach creates a new anonymous function using the `Function` constructor and executes it. 3. `new Function("use strict"; return 2 * 3")`: This approach creates a new anonymous function with the `"use strict"` directive and executes it. **Options Compared** The benchmark is comparing these three approaches in terms of their execution speed. The test cases are: * `eval("2 * 3");` * `func1();` (created using `new Function("return 2 * 3")`) * `func2();` (created using `new Function("use strict"; return 2 * 3")`) **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **eval**: Fast, but prone to security issues due to its ability to execute arbitrary code. Can also lead to performance issues if not used carefully. * `new Function`: Creates a new scope and is generally safer than `eval`, but can be slower due to the overhead of creating a new function object. * `new Function("use strict"; ...)`: Similar to `new Function`, but with the added benefit of enforcing strict mode, which can improve performance and security. **Library: Function** The `Function` constructor is a built-in JavaScript library that allows you to create anonymous functions. It's commonly used for creating simple functions without declaring them explicitly. **Special JS Feature/Syntax: Strict Mode** The `"use strict"` directive is a special feature in JavaScript that enables strict mode, which improves code security and performance by enforcing certain rules and restrictions. In this benchmark, the `new Function("use strict"; ...)` approach uses strict mode to improve performance and security. **Other Alternatives** If you're looking for alternatives to these approaches, here are a few options: * **Arrow functions**: For simple cases, arrow functions (e.g., `() => 2 * 3`) can be faster than traditional function expressions. * **Closures**: Closures (e.g., `(function() { return 2 * 3; })()`), created using the `Function` constructor or other means, can also be used as alternatives. * **Native functions**: Depending on the use case, native functions like `Math.multiply(2, 3)` might be faster than creating a custom function. Keep in mind that these alternatives might not provide the same level of control or flexibility as the original approaches.
Related benchmarks:
eval vs new Function #2
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?