Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs direct execution
(version: 0)
Comparing performance of:
eval vs new Function vs literal function
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var funcFromNew = new Function("return 2 * 3"); var funcFromLiteral = function() { return 2 * 3 };
Tests:
eval
eval("2 * 3");
new Function
funcFromNew();
literal function
funcFromLiteral();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
literal function
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches to execute a simple arithmetic expression: `2 * 3`. The approaches are: 1. Direct execution using JavaScript literals (`funcFromLiteral`) 2. Using the `new Function` constructor (`funcFromNew`) 3. Using the `eval()` function **Options Compared** The benchmark compares the performance of these three approaches in terms of the number of executions per second. **Pros and Cons of Each Approach** 1. **Direct Execution using JavaScript literals (funcFromLiteral)**: * Pros: Fastest, as it directly executes the code without any overhead. * Cons: Limited to simple expressions; cannot handle complex logic or conditional statements. 2. **Using the `new Function` constructor (funcFromNew)**: * Pros: Can handle more complex expressions and logic, as it allows for function declarations and definitions. * Cons: Slower than direct execution, due to the overhead of creating a new function object. 3. **Using the `eval()` function**: * Pros: Can evaluate arbitrary JavaScript code, including complex expressions and logic. * Cons: Slowest, due to the security features built into modern browsers, which restrict the use of `eval()` for security reasons. **Library Usage** The benchmark uses the following library: 1. `Function` constructor (`funcFromNew`): Creates a new function object with the given code as its body. 2. `eval()` function: Evaluates the given code and returns its result. **Special JS Features or Syntax** None mentioned in this specific benchmark, but note that some JavaScript features, such as `let` and `const`, were introduced in ECMAScript 2015 (ES6) and may be used in more complex benchmarks. **Other Alternatives** Alternative approaches to executing simple arithmetic expressions might include: 1. **Template literals**: A newer syntax for creating strings with embedded expressions. 2. **Arrow functions**: A concise way to create small, single-expression functions. 3. **Function expressions**: Similar to `new Function`, but can be used in a more flexible way. However, these alternatives are unlikely to provide significant performance improvements over the three approaches being compared in this benchmark.
Related benchmarks:
eval vs new Function v3
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?