Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function creation from text - eval vs new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
eval
var x = eval("(function (a,b){ return a * b})"); x(4,5)
new Function
var func = new Function("a","b","return a * b"); func(4,5);
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'd be happy to explain the JavaScript microbenchmark you provided. **Benchmark Overview** The benchmark compares two approaches for creating and executing a simple mathematical function in JavaScript: 1. `eval` 2. `new Function` **What is being tested?** In this benchmark, we're measuring the execution time of creating a function from text using both `eval` and `new Function`. The function takes two arguments, multiplies them together, and returns the result. **Options compared** The main difference between these two approaches is how they handle the creation and execution of the function: * `eval`: This method parses the provided string as JavaScript code and executes it immediately. In this case, we're passing a string literal that defines a function with two parameters. * `new Function`: This method creates a new function object dynamically using the provided string. The newly created function can then be invoked like any other function. **Pros and Cons** Here are some pros and cons of each approach: ### eval Pros: * More flexible, as you can pass arbitrary JavaScript code * Can be faster for simple expressions (since it's just parsing and executing) Cons: * Security risks if the input string is not sanitized or validated * Can lead to performance issues with large or complex expressions * Less control over the creation process ### new Function Pros: * Provides more control over the function creation process * Avoids security risks by executing a separate, sandboxed code block * More predictable and efficient for small, well-defined functions Cons: * Requires additional overhead due to creating a new function object * May be slower than `eval` for very simple expressions **Library usage** In this benchmark, no external library is used. **Special JS feature or syntax** There are no special JavaScript features or syntaxes mentioned in the provided benchmark. **Alternatives** If you need to create and execute functions dynamically, other alternatives could include: * `Function constructor`: A similar approach to `new Function`, but with more flexibility and control. * `Abstract Syntax Tree (AST) libraries**: Libraries like esprima or acorn can parse JavaScript code into an AST, which can then be executed using a custom interpreter. * **Just-In-Time (JIT) compilers**: Some frameworks, like React or Angular, use JIT compilation to optimize performance. While not directly applicable to this benchmark, they demonstrate the importance of efficient function creation and execution. **Benchmark preparation code** The provided `Script Preparation Code` is empty, which means that the benchmark script should be defined in a separate file or string literal. The `Html Preparation Code` is also empty, suggesting that the benchmark doesn't rely on any specific HTML structure. For this particular benchmark, you can create a simple JavaScript function and pass it to `eval` or `new Function`, like so: ```javascript var func = eval("(function (a,b){ return a * b})"); func(4, 5); ``` Or, using `new Function`: ```javascript var func = new Function("a", "b", "return a * b;"); func(4, 5); ``` These scripts can be executed by MeasureThat.net to generate the benchmark results.
Related benchmarks:
eval vs new Function
eval vs new Function v3
eval vs new Function #2
eval vs new Function (fix)
Comments
Confirm delete:
Do you really want to delete benchmark?