Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs Function with function creation at runtime
(version: 0)
Comparing performance of:
eval vs new Function
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
eval
eval("2 * 3")
new Function
Function("return 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):
Let's break down what's being tested in this benchmark. **What is being tested?** The benchmark compares two approaches to evaluating and executing JavaScript code: 1. `eval()`: A built-in JavaScript function that parses the string as JavaScript code and executes it. 2. Creating a new `Function` object with a runtime-constructed string using the syntax `new Function("expression")()`. **Options compared:** The benchmark is comparing the performance of these two approaches in executing simple arithmetic expressions (e.g., `2 * 3`). **Pros and Cons:** 1. **eval()**: * Pros: + Simple to use, as it requires only a string. + Fast execution, since it's a built-in function. * Cons: + Can be vulnerable to security issues (e.g., code injection) if not used carefully. + May have performance issues due to the parsing and execution overhead. 2. **new Function()**: * Pros: + More secure, as it doesn't execute arbitrary code directly. + Can provide better performance control, since the expression is parsed only once. * Cons: + Requires more code to use (constructing a `Function` object). + May have slower execution due to the parsing and evaluation overhead. **Other considerations:** Both approaches can be affected by factors like JavaScript engine optimizations, caching, and input data. The benchmark results may also depend on the specific JavaScript engine implementation used in each browser. **Library or feature used:** None are explicitly mentioned in this benchmark definition. **Special JS features or syntax:** The `eval()` function uses a special syntax, as it allows executing arbitrary JavaScript code from a string. However, since the benchmark is comparing two simple arithmetic expressions, no other specialized syntax is relevant here. Now, let's look at some alternatives: Alternatives to `eval()` could be: * Using `Function` constructors with runtime-constructed strings (similar to the second test case in this benchmark). * Employing libraries like `esprima` or `recast` for parsing and executing JavaScript code. * Using alternative evaluation methods, such as using `JSON.parse()` and `eval()` separately or employing a custom parser. Alternatives to creating a new `Function` object could be: * Using `Bind()` or `Apply()` to execute functions with dynamic arguments. * Employing libraries like `lodash.function` for functional programming. * Utilizing more advanced JavaScript features, such as arrow functions or `this` binding. Keep in mind that these alternatives may have different performance characteristics, security implications, or use cases compared to the approaches used in this benchmark.
Related benchmarks:
eval vs new Function #2
window.eval function vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
eval vs evalFunction vs function
Comments
Confirm delete:
Do you really want to delete benchmark?