Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs defined function
(version: 0)
Comparing performance of:
eval vs new Function vs Defined function
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3"); function func2() { return 2*3; }
Tests:
eval
eval("2 * 3");
new Function
func();
Defined function
func2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
Defined 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):
**Overview of the Benchmark** The provided benchmark measures the performance of three different ways to execute a simple mathematical expression: `2 * 3`. The goal is to determine which approach is the fastest. **Options Compared** 1. **`eval`**: Evaluates a string as JavaScript code using the `eval()` function. 2. **`new Function`**: Creates a new function using the `Function` constructor and calls it with the desired arguments. 3. **Defined function**: Creates a named function that performs the calculation. **Pros and Cons of Each Approach** 1. **`eval`**: * Pros: Easy to implement, simple syntax. * Cons: + Slow due to string evaluation and execution of user-supplied code. + Vulnerable to security risks if used with untrusted input. 2. **`new Function`**: * Pros: Faster than `eval`, as it compiles the function beforehand. * Cons: + Requires creating a new function object, which can be slower due to memory allocation and garbage collection. + Less intuitive syntax than defined functions. 3. **Defined function**: * Pros: + Fastest approach, as it uses an existing function that has already been optimized by the browser. + Most intuitive syntax, as it resembles traditional JavaScript function declaration. * Cons: None significant in this benchmark. **Library and Purpose (if applicable)** In this benchmark, none of the test cases use any external libraries. The only notable functionality is the use of `new Function` to create a new anonymous function (`func`), which is not a built-in JavaScript feature but rather a way to dynamically create functions. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark beyond what's typically available in modern browsers. **Other Alternatives** In theory, there are other approaches that could be used for simple calculations: 1. **Native arithmetic operators**: Using the `*` operator directly would likely be the fastest approach. 2. **BigInt support**: If the calculation involves very large numbers, using BigInts (a built-in JavaScript feature) might provide better performance. However, these alternatives are unlikely to offer significant benefits in this particular benchmark, as they involve more complex operations and may not be supported by older browsers or environments. **Benchmark Preparation Code Explanation** The `Script Preparation Code` section defines two functions: `func`, created using `new Function`, and `func2`, a named function that performs the same calculation. The `Html Preparation Code` section is empty, indicating that no HTML-related setup is necessary for this benchmark. In summary, the benchmark measures the performance of three approaches to executing simple mathematical expressions: `eval`, `new Function`, and defined functions. While each approach has its pros and cons, defined functions are likely to be the fastest due to their use of optimized existing code.
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?