var funcFunctionStr = "return 2 * 3";
var funcStr = "2 * 3";
eval(funcStr);
new Function(funcStr)();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
eval | |
new Function |
Test name | Executions per second |
---|---|
eval | 2218694.2 Ops/sec |
new Function | 101048.4 Ops/sec |
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:
eval
functionFunction
object using the new Function
syntaxComparison Options:
eval()
function, which executes the provided string as JavaScript code.Function
object from the provided string and then calls it.Pros and Cons of Each Approach:
eval('...' + user_input)
), as it can execute arbitrary JavaScript code.eval
, as it doesn't execute arbitrary codeLibrary/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:
2 * 3
. However, it's not as widely supported as eval
and may have performance implications.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.