Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs function
(version: 0)
Comparing performance of:
eval vs new Function vs function
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3"); var regular = function(){ return 2 * 3; }
Tests:
eval
eval("2 * 3");
new Function
func();
function
regular();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
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 its test cases. **Benchmark Definition** The benchmark definition provides three options for testing: `eval`, `new Function`, and `function`. These options are being compared to measure their performance in different scenarios. **Options Compared** 1. **`eval`**: The `eval` function is used to execute a string as JavaScript code. It's a powerful but potentially unsafe feature, as it can lead to code injection vulnerabilities. 2. **`new Function`**: This option uses the `Function` constructor to create a new function that takes a string of code as an argument and executes it. It provides more control over the execution environment than `eval`. 3. **`function`**: This option creates a new anonymous function using the syntax `function(){...}`. It's similar to `new Function`, but without the need for the explicit constructor. **Pros and Cons** * **`eval`**: + Pros: Easy to use, fast execution. + Cons: Potentially unsafe due to code injection vulnerabilities, slower performance in some cases. * **`new Function`**: + Pros: More control over the execution environment, safer than `eval`, potentially faster performance. + Cons: Slightly more complex to use, may require additional setup for error handling. * **`function`**: + Pros: Simple and intuitive syntax, fast execution, safe. + Cons: Less control over the execution environment compared to `new Function`. **Libraries and Special Features** None of the test cases use any external libraries. However, they do rely on JavaScript's built-in features: * **`Function`**: A constructor used to create new functions. * **`eval`**: A function that executes a string as JavaScript code. **Special Syntax** The `function` option uses the syntax `function(){...}`, which is a shorthand for creating an anonymous function. This syntax is more concise and readable than using the `Function` constructor or `new Function`. **Alternatives** Other alternatives to these options include: * **`let`/`const` functions**: Instead of using the `function` keyword, you can define functions using the `let`/`const` keywords followed by parentheses. * **Arrow functions**: A shorter syntax for defining small, one-time use functions: `(x) => { ... }`. * **Immediately Invoked Function Expressions (IIFE)**: A way to execute a function immediately after it's defined: `(function(){...})()`.
Related benchmarks:
eval vs new Function vs regular function
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?