Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs function vs creation
(version: 0)
Comparing performance of:
eval vs new Function vs function vs creation
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; }; var creation = function () { var func = new Function("return 2 * 3"); return func(); };
Tests:
eval
eval("2 * 3");
new Function
func();
function
regular();
creation
creation();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
eval
new Function
function
creation
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):
**What is being tested?** The provided JSON represents a JavaScript microbenchmarking test suite that compares the performance of four different ways to define and execute a simple expression: `eval()`, `new Function()`, `function` keyword, and "creation" (a custom implementation). Each benchmark definition in the test cases uses one of these methods to evaluate or execute the expression `2 * 3`. The goal is to measure which method performs best in terms of execution speed. **Options compared** The four options being tested are: 1. **eval()**: A built-in JavaScript function that evaluates a string as JavaScript code. 2. **new Function()**: Creates a new function with the given string as its body, and then executes it. 3. **function keyword**: Uses the `function` keyword to define an anonymous function, which is then executed. 4. **creation**: A custom implementation that defines a function using `new Function()` and then immediately calls it. **Pros and cons of each approach** 1. **eval()**: * Pros: Simple to use, no need to declare a variable for the function. * Cons: Can be slower than other approaches due to its dynamic nature, potential security risks if used with untrusted input. 2. **new Function()**: * Pros: More efficient than `eval()` and provides better control over function execution, but still has some overhead. * Cons: Requires declaring a variable for the function before using it. 3. **function keyword**: * Pros: Fastest way to define an anonymous function, no need to declare a variable beforehand. * Cons: Can be less efficient than `new Function()` if used with complex expressions or multiple function calls. 4. **creation**: * Pros: Provides better control over function execution and avoids some overhead of `new Function()`, but still has more complexity than other approaches. * Cons: Requires declaring a variable for the function before using it, which can be less convenient. **Other considerations** The benchmark's results are influenced by factors such as: * JavaScript engine optimizations * Browser-specific features or extensions (e.g., Chrome's performance optimization techniques) * Platform-specific differences in execution speed **Library usage** There is no explicit library usage mentioned in the provided JSON. However, `new Function()` creates a new function with a given string as its body, which implies the use of JavaScript's built-in `Function` constructor. **Special JS features or syntax** No special JavaScript features or syntax are used beyond what's required for the benchmark itself (e.g., `eval()`, function definition using the `function` keyword). **Alternatives** Alternative approaches to define and execute expressions include: * Using arrow functions (`=>`) instead of traditional functions * Utilizing modern JavaScript features like async/await or generator functions * Employing libraries like Node.js's built-in `vm` (Virtual Machine) module for executing user-defined code However, the benchmark seems to focus on evaluating the performance differences between these four specific approaches rather than exploring alternative methods.
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?