Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function (including parse)
(version: 0)
Comparing performance of:
eval vs new Function
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
eval
eval('(function test() { return 2 * 3 })()')
new Function
(new Function("return 2 * 3"))()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
new 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 break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark is designed to compare two different approaches: `eval` and `new Function`. The goal is to measure which approach is faster for executing small JavaScript functions. **Options Compared** The two options being compared are: 1. **`eval`**: A built-in JavaScript function that parses and executes a string as JavaScript code. 2. **`new Function` (or `Function()` constructor)**: A way to create a new, anonymous function dynamically using the `Function` constructor. **Pros and Cons** * **`eval`**: + Pros: - Simple to use - Works well for small, simple expressions + Cons: - Can be vulnerable to security risks (e.g., code injection) if not used carefully - May be slower than `new Function` due to the overhead of parsing and executing the string as JavaScript code * **`new Function`**: + Pros: - More secure than `eval`, as it prevents code injection attacks - Can be faster than `eval`, especially for larger expressions, since it avoids the overhead of parsing and executing the string as JavaScript code + Cons: - Requires a bit more boilerplate code to define the function (e.g., using template literals or string formatting) - May be slower than `eval` for very small expressions due to the creation overhead of the new function object **Library/Feature Considerations** There is no library used in this benchmark. However, it's worth noting that other approaches, like `Function()` with a single argument (e.g., `new Function('return 2 * 3;')`), can also be used to create dynamic functions. **Special JS Feature/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The focus is on the performance comparison between `eval` and `new Function`. **Other Alternatives** If you need to compare other approaches, here are a few alternatives: * **`Function()` with multiple arguments**: Similar to `new Function`, but allows passing arguments when creating the function. * **`Array.prototype.push.call` or similar methods**: Can be used to create dynamic functions in a more concise way (e.g., using `push.apply(function(arr) { arr[0] = 2; }([])`). * **ES6 template literals**: Can be used to create simple, one-liner expressions as strings (e.g., `eval('(() => 2 * 3)(())')`). Keep in mind that these alternatives might not provide the same performance benefits as `new Function`, and some may have different trade-offs in terms of security or conciseness.
Related benchmarks:
eval vs new Function #2
eval() vs new Function()
eval vs new Function (fix)
eval vs evalFunction vs function
Comments
Confirm delete:
Do you really want to delete benchmark?