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 vs new Function2
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
eval
eval("var str = '', i = 0; for(;i<1000;i++) { str += 'a';}");
new Function
Function("var str = '', i = 0; for(;i<1000;i++) { str += 'a';}")();
new Function2
Function("var str = '', i = 0; for(;i<1000;i++) { str += 'a';} return str;")();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
new Function2
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 break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of `eval()` versus creating a new function using the `Function` constructor. The `eval()` function is used to execute a string as JavaScript code, while the `Function` constructor creates a new function from a given string. **Options Compared** Two options are compared: 1. **eval()**: This option uses the built-in `eval()` function to execute the provided string as JavaScript code. 2. **new Function()**: This option uses the `Function` constructor to create a new function from the provided string. **Pros and Cons of Each Approach** * **eval()**: + Pros: Can be more concise and easier to write, especially for simple expressions or statements. + Cons: - Performance overhead due to parsing and execution of the code. - Security risks if not used carefully (e.g., executing user-provided input). * **new Function()**: + Pros: - Better performance since it avoids the overhead of `eval()`. - More control over the creation of the function, including access to its properties and methods. + Cons: - May be less concise than using `eval()` for simple expressions or statements. **Library and Purpose** The `Function` constructor is a built-in JavaScript object that creates a new function from a given string. It's used to dynamically create functions without requiring explicit function declarations. In the provided benchmark, the `new Function` tests use the constructor with two slightly different syntaxes: 1. **new Function()**: This is the simplest form of creating a new function using the `Function` constructor. 2. **new Function("...")()**: This syntax creates a new function and immediately calls it (i.e., executes the function body). **Special JS Feature or Syntax** The benchmark doesn't use any special JavaScript features or syntax that would require additional explanation. **Other Considerations** * The benchmark is run in a desktop environment on Mac OS X 10.15.6, which might affect the results. * The `RawUAString` field indicates the raw UA string sent by the browser to the server, but it's not directly relevant to the performance comparison. **Alternatives** Other alternatives for creating functions dynamically in JavaScript include: 1. **eval()**: As mentioned earlier, this is a more concise way to execute code, but with performance overhead. 2. **Function.prototype.toString()**: This method returns a string representation of the function, which can be used to create a new function using the `new` operator or other methods. In conclusion, the benchmark measures the performance difference between using `eval()` versus creating a new function using the `Function` constructor. The results indicate that `new Function()` is more efficient than `eval()`.
Related benchmarks:
eval vs new Function #2
eval() vs new Function()
eval vs new Function (fix)
window.eval function vs new Function
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?