Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function without cached parsing
(version: 0)
Comparing performance of:
eval vs new Function
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
eval
eval("2 * 3");
new Function
var func = new Function("return 2 * 3"); func();
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 help explain the JavaScript microbenchmark you've shared. **What is being tested?** MeasureThat.net is testing two different approaches for evaluating arithmetic expressions in JavaScript: `eval()` and `new Function()`. The benchmark compares the performance of these two methods in executing a simple arithmetic expression: `2 * 3`. **Options compared** The main options being compared are: 1. **`eval()`**: A built-in JavaScript function that executes arbitrary code, including scripts and expressions. 2. **`new Function()`**: A constructor function that creates a new function object from a string of code. **Pros and cons** * `eval()`: Pros: + Easy to use: just pass the expression as a string to `eval()`. + Simple syntax: no need to declare variables or define functions. Cons: + Performance overhead: `eval()` can be slower than other methods due to its dynamic nature. + Security risks: `eval()` can execute arbitrary code, making it a potential security risk if not used carefully. * `new Function()`: Pros: + More control: allows you to define the scope and context of the function. + Performance: often faster than `eval()` since it's just executing a simple function call. Cons: + More verbose: requires declaring variables and defining functions before execution. + Less readable: can be harder to understand the intent of the code. **Other considerations** * **Cached parsing**: MeasureThat.net is testing if parsed expressions are cached by either `eval()` or `new Function()`. This refers to whether the JavaScript engine caches the results of parsing an expression after it's been executed once. If caching occurs, re-executing the same expression may be faster. * **Browser differences**: The benchmark results show variations in performance across different browsers (in this case, Firefox 89 on a Windows desktop). This highlights the importance of testing in multiple environments to ensure your code is compatible and performs well. **Library and syntax** There are no external libraries used in these benchmarks. However, `new Function()` relies on JavaScript's built-in `Function` constructor, which is a standard feature in most modern browsers. **Special JS features or syntax** None of the benchmarking approaches rely on any special JavaScript features or syntax that would be unusual to encounter in everyday coding. The code snippets are straightforward and easy to understand. **Alternatives** If you're looking for alternative methods to evaluate expressions, consider: * **`let` expression**: A newer feature introduced in ECMAScript 2015 (ES6), which allows defining a variable without the `var`, `const`, or `let` keywords. * **Template literals**: Another ES6 feature that enables creating string templates with embedded expressions using backticks (`). * **`with` statement**: Although less common, some developers use the `with` statement to create a scope for an expression. Keep in mind that these alternatives might not be as widely supported or optimized as the `eval()` and `new Function()` approaches.
Related benchmarks:
eval vs new Function #2
eval() vs new Function()
eval vs new Function (fix)
eval vs new Function (including parse)
eval vs evalFunction vs function
Comments
Confirm delete:
Do you really want to delete benchmark?