Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function v3
(version: 0)
Comparing performance of:
eval vs new Function vs func
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("2 * 3");
new Function
func();
func
function fff() { return 2 * 3; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
func
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's being tested. **Benchmark Definition** The benchmark is defined in two parts: `Script Preparation Code` and `Html Preparation Code`. The latter is empty, so we'll focus on the former. The script preparation code is a piece of JavaScript that creates a new function using the `new Function()` constructor. This function takes a string literal as an argument and returns the result of evaluating that string as JavaScript code. In this case, the string is `"return 2 * 3"`. This means we have three test cases: 1. `eval("2 * 3");` - uses the built-in `eval()` function to execute the string as JavaScript code. 2. `func();` - calls a newly created function using the `new Function()` constructor, passing the same string literal as an argument. 3. `function fff() { return 2 * 3; }` - creates a new anonymous function using the function declaration syntax. **Options Compared** We're comparing three approaches: 1. **eval()**: executes a string as JavaScript code at runtime. 2. **new Function()**: creates a new function and calls it with the provided argument (in this case, the string literal). 3. **Function Declaration**: creates a new anonymous function using the function declaration syntax. **Pros and Cons** Here are some pros and cons of each approach: 1. **eval()**: * Pros: easy to use, flexible. * Cons: can be a security risk if used with untrusted input, performance overhead. 2. **new Function()**: * Pros: creates a new function that can be reused, avoids some security concerns of `eval()`. * Cons: requires a string literal as an argument, might not be as efficient as other approaches. 3. **Function Declaration**: * Pros: explicit, readable syntax, no overhead from creating a function object. * Cons: not suitable for all use cases (e.g., when you need to pass arbitrary code as an argument). **Libraries and Special JS Features** There are no libraries involved in this benchmark. **Special JS Features** There is one special feature being tested: 1. **Function Expressions**: The `new Function()` constructor uses function expressions, which were introduced in ECMAScript 5 (2015). This syntax allows creating anonymous functions that can be executed later. **Alternative Approaches** Some alternative approaches to benchmarking this code might include: * Using a more modern approach like using an Immediately Invoked Function Expression (IIFE): `(function() { var func = new Function("return 2 * 3"); return func(); })()` * Measuring the performance of different JavaScript engines or versions * Comparing the performance of `eval()` with other alternatives, such as `Function constructor` or `parser.parseScript` These alternative approaches would require modifications to the benchmark code and might provide more detailed insights into the performance characteristics of each approach.
Related benchmarks:
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?