Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function proper
(version: 0)
Comparing performance of:
eval vs new Function
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var funcFunctionStr = "return 2 * 3"; var funcStr = "2 * 3";
Tests:
eval
eval(funcStr);
new Function
new Function(funcStr)();
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 JavaScript microbenchmark. **Benchmark Purpose:** The benchmark is designed to compare the performance of two approaches for executing a simple arithmetic expression in JavaScript: 1. `eval` function 2. Creating a new `Function` object using the `new Function` syntax **Comparison Options:** * **eval**: This approach uses the built-in `eval()` function, which executes the provided string as JavaScript code. * **new Function**: This approach creates a new `Function` object from the provided string and then calls it. **Pros and Cons of Each Approach:** * **eval**: + Pros: - Easy to use, as it's a built-in function - Can be more convenient for simple expressions + Cons: - Can pose security risks if used with untrusted input (e.g., `eval('...' + user_input)`), as it can execute arbitrary JavaScript code. - May have performance overhead due to the interpreter's state and caching mechanisms. * **new Function**: + Pros: - Provides more control over the execution environment, as it creates a new scope and context - Can be safer than `eval`, as it doesn't execute arbitrary code + Cons: - Requires creating a new object and calling its method, which can add overhead. - May not be suitable for simple expressions, as it adds unnecessary complexity. **Library/Language Features Used:** None mentioned in this benchmark definition. The tests only use basic JavaScript syntax. **Special JS Feature/Syntax:** No special features or syntax are used in this benchmark. It's a straightforward comparison of two approaches. **Alternative Approaches:** * **String.prototype.parse()**: This method can be used to parse and execute simple arithmetic expressions, like `2 * 3`. However, it's not as widely supported as `eval` and may have performance implications. * **Abstract Syntax Trees (ASTs)**: Some modern JavaScript engines, like V8, use ASTs to optimize and compile code. This approach can provide better performance but is more complex to implement. In summary, this benchmark compares the performance of two approaches for executing simple arithmetic expressions in JavaScript: `eval` and creating a new `Function` object using the `new Function` syntax. The choice between these approaches depends on the trade-offs between convenience, security, and performance.
Related benchmarks:
toFixed vs Math.round()
toFixed() vs Math.round().toString()
toFixed() vs String(Math.floor()
parseFloat(toFixed) vs Math.round()
toFixed vs Math.round() with numbers222
Comments
Confirm delete:
Do you really want to delete benchmark?