Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function (Sample JS Functions)2
(version: 0)
Comparing performance of:
eval vs new Function vs navite
Created:
3 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();
navite
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
navite
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
2056169.0 Ops/sec
new Function
4150807.0 Ops/sec
navite
4818729.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and its implications. **Benchmark Overview** The `MeasureThat.net` benchmark compares the performance of three different approaches to execute a JavaScript function: 1. `eval` 2. `new Function` 3. `native` (i.e., using native JavaScript syntax) **What is being tested?** In each test case, a simple JavaScript function is defined and executed: * In `new Function`, a `Function` constructor is used to create a new function. * In `eval`, the `eval` function is used to execute a string containing the code. * In `native`, a native JavaScript syntax is used without the need for `new Function` or `eval`. **Options compared** The benchmark compares the performance of these three approaches on multiple browsers and devices. **Pros and Cons of each approach:** 1. **new Function** * Pros: + Can be used to create complex functions with closures. + Allows for flexibility in function creation. * Cons: + May incur a performance overhead due to the creation of a new function object. + May not be supported in older browsers or environments. 2. **eval** * Pros: + Fast execution, as it directly executes the code without creating an intermediate function object. + Can be used for simple functions with no closures. * Cons: + May introduce security risks if used with untrusted input. + Performance can degrade if the input code is large or complex. 3. **native** * Pros: + Fast execution, as it does not require creating an intermediate function object. + Supports closures and complex functions out-of-the-box. * Cons: + May be slower than `new Function` for simple functions without closures. **Library or special JS feature used:** None of the provided benchmarks use a specific library. However, it's worth noting that `eval` can be vulnerable to security risks if not used carefully (e.g., with untrusted input). The `native` approach is generally considered safer and more efficient for simple functions. **Special JS features or syntax:** The `native` approach uses native JavaScript syntax without the need for `new Function` or `eval`. This allows for flexible function creation while maintaining performance. Note that the exact syntax may vary depending on the JavaScript engine and version used. **Alternatives:** If you're interested in alternatives to this benchmark, consider: 1. **Function constructor vs. String.prototype.replace()**: Compares the performance of creating a new function using `Function` versus replacing a string with a new value. 2. **eval vs. Function()**: Similar to the original benchmark, but compares only `eval` and `Function()` without `native`. 3. **Arrow functions vs. traditional functions**: Tests the performance of arrow functions versus traditional functions in various scenarios. These alternative benchmarks can help you compare different JavaScript features and optimizations.
Related benchmarks:
eval vs new Function (Sample JS Functions)
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
Comments
Confirm delete:
Do you really want to delete benchmark?