Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
indirect eval vs new Function
(version: 0)
Benchmark.js
Comparing performance of:
indirect eval vs new Function
Created:
4 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var func = new Function('"use strict"; return 2 * 3');
Tests:
indirect eval
eval?.('"use strict"; 2 * 3');
new Function
func();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indirect eval
new Function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
new Function
11.8M/s
indirect eval
10K/s
View exact numbers
Test name
Executions per second
✓
new Function
11,773,138 Ops/sec
indirect eval
9,581 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, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing two approaches: 1. **Indirect Eval**: This involves evaluating a JavaScript expression directly using the `eval()` function. The test case uses the `?.` operator (optional chaining) to ensure that `eval()` is called only when the value inside it is not null or undefined. 2. **New Function**: This approach creates a new anonymous function using the `new Function()` constructor and executes it. **Script Preparation Code** The script preparation code defines a variable `func` as an anonymous function: ```javascript var func = new Function('\"use strict\"; return 2 * 3'); ``` This is done to create a local scope for the function, which can help prevent global variables from being created. **Options Compared** The two options are compared in terms of performance: * **Indirect Eval**: This approach involves calling `eval()` with an expression that returns the result. Since `eval()` has a slower execution time due to its overhead, indirect eval might be slower than creating a new function. * **New Function**: Creating a new anonymous function using `new Function()` can be faster since it avoids the overhead of `eval()`. However, this approach requires more memory and creates a new scope. **Pros and Cons** * **Indirect Eval**: + Pros: Easier to write and understand, as it's similar to evaluating an expression directly. + Cons: Slower due to the overhead of `eval()`. * **New Function**: + Pros: Faster execution time since it avoids `eval()`'s overhead, but requires more memory and creates a new scope. + Cons: More complex and harder to understand due to its additional complexity. **Library/Functionality Used** The test case uses the `?.` operator (optional chaining), which is a feature introduced in ECMAScript 2020. The `eval()` function is also used, but it's not specific to any particular library or framework. **Special JS Feature/Syntax** The `?.` operator is a new syntax in JavaScript, specifically designed for nullish coalescing and optional chaining. It's not compatible with older browsers that don't support this feature. **Other Alternatives** If the `new Function()` approach was not used, another alternative could be to use a native function like `Function.prototype.call()`, which would achieve similar results without creating a new anonymous function. However, this would require more memory and might not be as efficient as using `new Function()`. ```javascript var func = Function('"use strict"; return 2 * 3'); func(); ``` Keep in mind that these alternatives are not directly comparable to the original options, and their performance differences may vary depending on specific use cases.
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?