Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function (Sample JS Functions)
(version: 0)
Comparing performance of:
eval vs new Function
Created:
7 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();
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:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
4607927.5 Ops/sec
new Function
6408443.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing two approaches: `eval` and `new Function`. The goal is to measure which approach is faster, more efficient, or has better performance characteristics. **Options Compared** Two options are being compared: 1. **`eval`**: A built-in JavaScript function that evaluates a string as JavaScript code. 2. **`new Function`**: A constructor function used to create a new function object. The syntax `new Function("code")` creates a new function with the specified code. **Pros and Cons of Each Approach** 1. **`eval`**: * Pros: Can be convenient for evaluating simple expressions or executing strings as JavaScript code. * Cons: Can be slow, as it involves parsing and compiling the code string, which can lead to performance issues with complex or large code snippets. Additionally, `eval` can also introduce security risks if used with untrusted input. 2. **`new Function`**: * Pros: Provides more control over the creation of function objects, allowing for better optimization and caching. It's generally considered a safer option than `eval`, as it avoids the risk of executing arbitrary code. * Cons: Requires specifying the full function syntax, which can be cumbersome for simple expressions. **Library Usage** In this benchmark, the `Function` constructor is used to create a new function object. This is a built-in JavaScript library, and its purpose is to provide a way to dynamically create function objects from strings or other data sources. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being used in these test cases. The benchmark focuses on the performance comparison between `eval` and `new Function`. **Other Alternatives** If you're interested in exploring alternative approaches for executing code, here are a few options: 1. **`Function.prototype.toString()`**: This method returns a string representation of the function, which can be used to create a new function object. 2. **`bind()` or `call()` methods**: These methods allow you to invoke functions with specific arguments and contexts. However, these alternatives are not being compared in this benchmark. **Benchmark Preparation Code** The preparation code for each test case is provided: * For the `eval` test case: `let r = Math.random().toString(36).substring(7).includes('x');` * For the `new Function` test case: `var newFunc = new Function("let r = Math.random().toString(36).substring(7).includes('x');");` These code snippets are used to define a simple expression that tests the performance of each approach. **Latest Benchmark Result** The latest benchmark results show: * For the `new Function` test case: 438,8521.5 executions per second * For the `eval` test case: 178,9463.375 executions per second These results suggest that `new Function` is significantly faster than `eval` in this specific benchmark scenario. I hope this explanation helps you understand what's being tested in this benchmark!
Related benchmarks:
eval vs new Function vs 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?