Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs function (Sample JS Functions)
(version: 0)
Comparing performance of:
eval vs new Function vs func
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var newFunc = new Function("let r = Math.random().toString(36).substring(7).includes('x');");
Tests:
eval
eval("let r = Math.random().toString(36).substring(7).includes('x');");
new Function
newFunc();
func
let r = Math.random().toString(36).substring(7).includes('x');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
func
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
3129476.8 Ops/sec
new Function
4751857.0 Ops/sec
func
4726098.5 Ops/sec
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: 1. **`new Function()`**: This approach creates a new function using the `Function` constructor, which takes a string of JavaScript code as an argument. 2. **`eval()`**: This approach uses the built-in `eval()` function to execute a string of JavaScript code. 3. **`let r = Math.random().toString(36).substring(7).includes('x');`** (alias: `func`): This is a simple JavaScript expression that generates a random string and checks if it includes the character `'x'`. **Options Compared** The benchmark compares the performance of these three approaches: * **`new Function()`**: Creating a new function using the `Function` constructor. * **`eval()`**: Using the built-in `eval()` function to execute JavaScript code. * **`func` (simple expression)**: Executing a simple JavaScript expression. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **`new Function()`**: * Pros: Can be more secure than `eval()`, as it prevents access to the global object. * Cons: Can be slower due to the overhead of creating a new function object. 2. **`eval()`**: * Pros: Fast and efficient, but can pose security risks if used with untrusted input. * Cons: Can be slow for large or complex code blocks, as it evaluates the entire expression. 3. **`func` (simple expression)**: * Pros: Simple to implement and fast, as it's just a single expression being evaluated. * Cons: Limited flexibility compared to `new Function()` or `eval()`, which can execute arbitrary code. **Library: None** There are no external libraries used in this benchmark. The test cases only rely on built-in JavaScript features. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. It's focused on comparing the performance of different approaches to executing JavaScript code. **Alternatives** If you're looking for alternatives to this benchmark, here are a few options: * **`Function()` constructor**: This is similar to `new Function()`, but with a more concise syntax. * **`Function Expression`**: This is another way to create functions in JavaScript, using the syntax `let func = function() { ... };`. * **`Arbitrary Code Execution`**: If you need to execute arbitrary code, you might consider using a framework like WebAssembly (WASM) or a sandboxed environment. Keep in mind that this benchmark is focused on comparing the performance of different approaches to executing JavaScript code. If your use case requires more complex scenarios, you may want to consider alternative benchmarks or testing frameworks.
Related benchmarks:
eval vs new Function (Sample JS Functions)
eval vs new Function vs function(Sample JS Functions)
new function vs eval vs regular functions
eval vs new Function (Sample JS Functions)2
Comments
Confirm delete:
Do you really want to delete benchmark?