Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs function(Sample JS Functions)2
(version: 0)
Comparing performance of:
eval vs new Function vs regular
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var newFunc = new Function("return Math.random().toString(36).substring(7).includes('x');"); var regular = function(){ return Math.random().toString(36).substring(7).includes('x');; }
Tests:
eval
eval("Math.random().toString(36).substring(7).includes('x');");
new Function
newFunc();
regular
regular()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
regular
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
2427326.0 Ops/sec
new Function
4986992.5 Ops/sec
regular
3444326.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark compares three different approaches to execute a simple JavaScript function: `eval`, `new Function`, and a regular function. **Script Preparation Code Explanation** The script preparation code is used to define two sample JavaScript functions: 1. `var newFunc = new Function("return Math.random().toString(36).substring(7).includes('x');");` This line defines a new function using the `Function` constructor, which takes an anonymous function as its argument. 2. `var regular = function(){ return Math.random().toString(36).substring(7).includes('x');};` This line defines a regular function using the `function` keyword. The purpose of these functions is to test the performance of different approaches to execute a simple JavaScript function that includes a random string and checks if it contains the character 'x'. **Options Compared** The benchmark compares three options: 1. **eval**: The `eval` function executes the code passed as a string. 2. **new Function**: As mentioned earlier, this approach uses the `Function` constructor to execute an anonymous function. 3. **regular (function)**: This approach uses a regular function defined using the `function` keyword. **Pros and Cons of Each Approach** 1. **eval**: * Pros: Fast execution and low overhead. * Cons: Can be vulnerable to security threats if used with untrusted input, and can execute arbitrary code due to its ability to execute strings as JavaScript code. 2. **new Function**: * Pros: Provides better protection against security threats compared to `eval`, and can execute anonymous functions securely. * Cons: May have higher overhead compared to `eval` due to the creation of a new function object. 3. **regular (function)**: * Pros: Provides good performance and is relatively secure, as it executes JavaScript code within a defined scope. * Cons: May not be suitable for all use cases where dynamic function execution is required. **Other Considerations** * **Library Usage**: None of the tested approaches rely on any external libraries. * **Special JS Features/Syntax**: The benchmark does not use any special JavaScript features or syntax, such as async/await, promises, or modern JavaScript language features. **Alternatives to These Approaches** If you need to execute dynamic code in your application, consider using: 1. **Function constructors (e.g., `new Function()`) for secure execution of anonymous functions. 2. **eval with strict mode** for a balance between security and performance. 3. **Regular function definitions** for predictable and secure execution. Keep in mind that each approach has its trade-offs and should be chosen based on the specific requirements and constraints of your application.
Related benchmarks:
eval vs new Function (Sample JS Functions)
eval vs new Function vs function (Sample JS Functions)
window.eval function vs new Function
window.eval function vs new Function1
Comments
Confirm delete:
Do you really want to delete benchmark?