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 regular
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');"); var regular = function(){ return Math.random().toString(36).substring(7).includes('x');; }
Tests:
eval
eval("let r = 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:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
1452304.4 Ops/sec
new Function
4180162.0 Ops/sec
regular
4197604.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. **Benchmark Overview** The benchmark consists of three test cases: `eval`, `new Function`, and `regular`. The goal is to compare their performance in executing a specific JavaScript function. We'll break down each test case, explain what's happening under the hood, and discuss pros and cons. **Test Case 1: eval** `eval` is a built-in JavaScript function that executes a string as if it were code. In this test case, the string `"let r = Math.random().toString(36).substring(7).includes('x');;"` defines a variable `r` using a random value generated by `Math.random()`. The `eval` function then evaluates and returns the result of that expression. **Pros:** * Simple to implement * Fast execution (since it's just interpreting code) **Cons:** * Potential security risks, as `eval` can execute arbitrary code * May be slower due to the overhead of parsing and executing the string **Test Case 2: new Function** In this test case, a new function is created using the `new Function` syntax. The same string `"let r = Math.random().toString(36).substring(7).includes('x');;"` defines a variable `r`, just like in the `eval` test case. **Pros:** * Similar to `eval`, but with better security guarantees * May be faster due to the compiler optimization of the generated code **Cons:** * Still involves function creation, which can incur overhead * Not as simple to implement as `eval` **Test Case 3: regular** This test case uses a named function expression instead of `new Function` or `eval`. The same string `"let r = Math.random().toString(36).substring(7).includes('x');;"` defines a variable `r`, just like in the other two test cases. **Pros:** * Similar to `eval` and `new Function` * May be faster due to the compiler optimization of the generated code **Library/Function Usage** None of these test cases use any external libraries or functions. The focus is on comparing the performance of different JavaScript features. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's a straightforward comparison of three simple execution methods. **Alternative Approaches** Other approaches to executing code could include: * `String.prototype.execute()` (if supported by the browser) * `Function()`, without using `new` * Compiler-based optimizations, such as ES6 modules or WebAssembly * Native code generation and execution (e.g., using WebAssembly) Keep in mind that these alternatives might not be supported by all browsers or environments. MeasureThat.net's focus is on comparing JavaScript features, so it uses a more controlled and standardized approach. I hope this explanation helps you understand what's happening in the MeasureThat.net benchmark!
Related benchmarks:
eval vs new Function (Sample JS Functions)
eval vs new Function vs function (Sample JS Functions)
eval vs new Function (Sample JS Functions)2
eval vs new Function vs function(Sample JS Functions)2
Comments
Confirm delete:
Do you really want to delete benchmark?