Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function
(version: 0)
Comparing performance of:
eval vs new Function
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("2 * 3");
new Function
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:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
36264216.0 Ops/sec
new Function
268110016.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches: using `eval()` and creating a new function with `new Function()`. The script preparation code for the first approach is: ```javascript var func = new Function("return 2 * 3"); ``` This creates a new function that takes no arguments and returns the result of evaluating "2 * 3". **Script Preparation Code** The script preparation code for the second approach is simply: ```javascript eval("2 * 3"); ``` This uses the built-in `eval()` function to evaluate the expression. **What's being tested?** The benchmark is measuring the performance difference between these two approaches. Specifically, it's testing: * How long it takes to execute each function (in terms of CPU cycles or time) * Which approach has a faster execution rate per second **Options compared** There are only two options being compared in this benchmark: `eval()` and `new Function()`. Both approaches evaluate the expression "2 * 3", but they do so in different ways. **Pros and Cons of each approach** * **`eval()`**: + Pros: - Simple to use - Can be used for arbitrary expressions + Cons: - Can execute arbitrary code, making it a security risk if not used carefully - Can be slower due to the overhead of parsing and executing the expression * **`new Function()`**: + Pros: - More controlled execution environment (can't execute arbitrary code) - May be faster due to the ability to optimize the generated function + Cons: - Requires more code and setup compared to `eval()` **Library used** There is no explicit library being used in this benchmark. However, it's worth noting that some browsers may have additional optimizations or features that affect the performance of these functions. **Special JS feature or syntax** Neither of the approaches uses any special JavaScript features or syntax beyond basic arithmetic expressions. **Other alternatives** If you wanted to add more options to the benchmark, you could consider adding other ways to evaluate expressions, such as: * Using a library like `escodegen` or `recast` to generate and optimize the expression * Comparing the performance of different JavaScript engines (e.g., V8 vs SpiderMonkey) * Evaluating expressions in different contexts (e.g., with and without sandboxing) Keep in mind that adding new options would require additional benchmark preparation code, test cases, and results analysis. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
eval vs new Function #2
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?