Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs function 1337
(version: 0)
Comparing performance of:
eval vs new Function vs function in code
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return 2 * 3"); function funcs() { return 2 * 3; }
Tests:
eval
eval("2 * 3");
new Function
func();
function in code
funcs();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
function in code
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 the provided benchmark definition and test cases to understand what's being tested. **Benchmark Definition JSON** The benchmark is designed to compare the performance of three different approaches for evaluating or creating a function in JavaScript: 1. `eval` 2. `new Function` 3. `function` (in code) **Script Preparation Code** The script preparation code defines two functions: `func` and `funcs`. Both functions return the result of multiplying 2 by 3. * `func`: Created using the `new Function` constructor, which takes a string literal as an argument. * `funcs`: Defined directly in the JavaScript code using the `function` keyword. **Html Preparation Code** The html preparation code is empty, indicating that no HTML elements or interactions are being tested. **Individual Test Cases** There are three test cases: 1. **eval**: Tests the performance of using `eval` to evaluate a string literal. 2. **new Function**: Tests the performance of creating a function using the `new Function` constructor. 3. **function in code**: Tests the performance of defining a function directly in the JavaScript code using the `function` keyword. **Libraries and Special Features** None of the test cases use any external libraries or special features like ES6 syntax, async/await, or modern language features. **Pros and Cons of Each Approach** Here's a brief summary of each approach: 1. **eval**: Pros: Simple to implement, works with most JavaScript engines. Cons: * Performance overhead due to parsing and execution. * Security risks if used with untrusted input. 2. **new Function**: Pros: More secure than `eval`, provides a sandboxed environment for the function. 3. **function in code**: Pros: Most efficient way to define functions, eliminates security concerns. **Benchmark Results** The latest benchmark results show that: 1. `function in code` is the fastest approach, with an execution rate of 27557826 executions per second. 2. `new Function` is slightly slower, with an execution rate of 27492260 executions per second. 3. `eval` is the slowest approach, with an execution rate of 5816247 executions per second. **Alternatives** If you're looking for alternative ways to benchmark or compare function creation approaches in JavaScript, consider: 1. Using a testing framework like Jest or Mocha. 2. Writing custom benchmarks using Node.js's built-in `perf_hooks` module. 3. Comparing the performance of different engines (e.g., V8 vs SpiderMonkey) using tools like benchmarking frameworks. Keep in mind that this is just one example of how to create a JavaScript benchmark, and there are many variations depending on your specific use case and requirements.
Related benchmarks:
eval vs new Function v3
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?