Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval'd function vs. new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func1 = new Function("a", "b", "return a * b"); var func2 = eval("(function (a, b) { return a * b; })");
Tests:
eval
func1(2, 3);
new Function
func2(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):
I'll break down the benchmark and explain what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark compares two ways to create and execute a simple mathematical function: `eval` and `new Function`. The test cases are designed to measure which method is faster and more efficient. **Script Preparation Code** In this code, we have: ```javascript var func1 = new Function("a", "b", "return a * b"); var func2 = eval("(function (a, b) { return a * b; })"); ``` Here's what's happening: * `new Function` creates a new function object with the specified parameters and body. In this case, it takes two arguments `a` and `b`, and returns their product. * `eval` is used to evaluate the provided string as JavaScript code. The string defines an anonymous function that takes two arguments `a` and `b` and returns their product. **Test Cases** The benchmark has two test cases: 1. **`eval`**: This test case calls the `eval` function with the string `(function (a, b) { return a * b; })`. The goal is to measure how fast this code executes. 2. **`new Function`**: This test case creates a new function object using the `new Function` syntax and then calls it with the arguments `2` and `3`. **Library and Features** * `eval` uses the built-in JavaScript `eval` function, which is an implementation of the Turing complete Scheme dialect. The use of `eval` can be considered as a security risk if not used carefully, as it can evaluate arbitrary JavaScript code. * No special JS features or syntax are used in this benchmark. **Pros and Cons** * **`new Function`**: + Pros: This approach creates a new function object that is optimized for execution. It avoids the overhead of `eval`. + Cons: Creating a new function object requires more memory allocation than using `eval`. * **`eval`**: + Pros: Using `eval` allows for more flexibility and can be faster in some cases, as it doesn't require creating a new function object. + Cons: As mentioned earlier, using `eval` can pose security risks if not used carefully. **Benchmark Results** The latest benchmark results show that the `new Function` approach is slightly faster than `eval`. However, this may depend on various factors such as the specific JavaScript engine, hardware, and other code in the benchmark. **Other Alternatives** If you're looking for alternatives to these approaches: * Instead of using `eval`, you can use a library like `parse` or `Function` from `es5-shim` to parse and execute the provided string. * If performance is critical, you can use native code compilation tools like Emscripten or V8's `toNativeCode()` function to generate optimized machine code. Keep in mind that these alternatives may come with their own trade-offs and requirements.
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?