Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new func
(version: 0)
Comparing performance of:
eval vs new Function
Created:
6 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 explain the JavaScript microbenchmark provided by MeasureThat.net. **Benchmark Overview** The benchmark compares two ways of performing arithmetic multiplication in JavaScript: using the `eval()` function and creating a new anonymous function with the `new Function()` constructor. **Options Compared** There are two options being compared: 1. **`eval("2 * 3");`**: This uses the `eval()` function to execute a string as JavaScript code. 2. **`var func = new Function("return 2 * 3"); func();`**: This creates a new anonymous function using the `new Function()` constructor and then calls it, effectively performing the same arithmetic operation. **Pros and Cons** **`eval()`** Pros: * Simple to use: just pass the desired expression as a string. * Fast execution: since `eval()` is optimized for execution, it can be quite fast. Cons: * Security risk: since `eval()` executes arbitrary code, it can pose security risks if used with untrusted input. * Performance variability: browser implementations may optimize `eval()` differently, affecting performance consistency. * Limited control: once the string is passed to `eval()`, there's no way to modify or inspect its behavior. **`new Function()`** Pros: * Control: you can easily modify or inspect the generated function. * Security: since a new function object is created, it doesn't pose security risks like `eval()` does. * Predictability: browser implementations tend to optimize `new Function()` consistently, making performance more predictable. Cons: * More verbose: creating an anonymous function requires more code than using `eval()`. * Potential overhead: parsing and compiling the new function can introduce additional overhead. **Other Considerations** When comparing these two approaches, keep in mind that both are intended for simple arithmetic operations like multiplication. For more complex expressions or performance-critical code, other methods may be more suitable. **Libraries and Special Features** There are no external libraries used in this benchmark. However, if you're interested in exploring libraries for JavaScript evaluation or function creation, some popular ones include: * `eval()` itself (built-in) * `Function` constructor (built-in) * `function` keyword ( built-in) * `es6-function-expression` library (optional) **Special JS Features** There are no special JavaScript features used in this benchmark that would require explanation. **Alternatives** If you need to compare performance of different JavaScript methods for arithmetic operations, consider alternatives like: 1. **Paradox.js**: a microbenchmarking library designed specifically for JavaScript. 2. **Benchmark.js**: another popular microbenchmarking library with a focus on simplicity and flexibility. 3. **V8 Benchmark Suite** (for V8-only browsers): provides a set of pre-built benchmarks for various JavaScript operations. In conclusion, the MeasureThat.net benchmark highlights the trade-offs between using `eval()` versus creating a new anonymous function using `new Function()`. While `eval()` can be simple to use and fast in execution, its limitations and potential security risks make it less desirable. In contrast, `new Function()` offers more control and predictability, but at the cost of verbosity and potential overhead.
Related benchmarks:
eval vs new Function v3
eval vs new Function #2
eval() vs new Function()
eval vs new Function (fix)
window.eval vs new Function
Comments
Confirm delete:
Do you really want to delete benchmark?