Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function 2asdfadsfsa
(version: 0)
Comparing performance of:
eval vs new Function vs func2();
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3"); var func2 = function() { return 2 * 3 };
Tests:
eval
eval("2 * 3");
new Function
func();
func2();
func2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
func2();
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):
I'd be happy to help you understand the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark compares the performance of three different approaches: `eval`, `new Function`, and a simple function declaration (`func2`). The goal is to determine which approach is fastest. **Script Preparation Code** The script preparation code defines two functions: 1. `var func = new Function("return 2 * 3");`: This creates a new `Function` object with the string `"return 2 * 3"` as its body. 2. `var func2 = function() { return 2 * 3 };`: This declares a simple anonymous function that returns the result of `2 * 3`. **Test Cases** There are three test cases: 1. `eval("2 * 3");`: This tests the performance of the built-in `eval` function. 2. `func();`: This tests the performance of the `new Function` object created in the script preparation code. 3. `func2();`: This tests the performance of the simple anonymous function declared in the script preparation code. **Library: `Function`** The `Function` constructor is a built-in JavaScript API that allows you to create new functions dynamically. It takes two arguments: a string containing the function body, and an optional `this` parameter (not used in this case). The purpose of this library is to enable dynamic creation of functions. **Special JS Feature: `eval`** The `eval` function is a built-in JavaScript API that evaluates a given string as JavaScript code. It's commonly used for parsing and executing strings containing JavaScript expressions. In this benchmark, `eval` is used to execute the string `"2 * 3"` directly. **Performance Comparison** The results show that: 1. `new Function` is significantly faster than `eval`, with an execution rate of approximately 10x higher. 2. The simple anonymous function (`func2`) is slower than both `new Function` and `eval`. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: * `eval`: + Pros: Simple to use, doesn't require creating a new function object. + Cons: Performance is generally slower due to its dynamic nature and potential security risks (e.g., code injection attacks). * `new Function`: + Pros: Faster performance, allows for better control over the creation process. + Cons: Requires creating a new function object, which can be more complex than using `eval`. * `func2`: (simple anonymous function) + Pros: Fast execution rate, concise syntax. + Cons: Requires declaring a separate variable, which may not be necessary in all cases. **Alternatives** If you need to create dynamic functions, you might consider alternatives like: 1. Using a library like Lodash or Underscore.js, which provide built-in `Function`-like APIs for creating functions. 2. Implementing your own function creation logic using techniques like function expressions (e.g., `var func = (function() { return 2 * 3; })();`). 3. Using a JavaScript engine that supports just-in-time compilation or Ahead-of-Time compilation, which can improve performance at the cost of complexity. I hope this explanation helps you understand the JavaScript microbenchmark on MeasureThat.net!
Related benchmarks:
eval vs new Function v3
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?