Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new function vs eval vs regular functions
(version: 0)
Comparing performance of:
Arrow function vs eval vs new function vs regular function
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Arrow function
var arrowFunc = () => {let r = Math.random().toString(36).substring(7).includes('x');} arrowFunc();
eval
eval("let r = Math.random().toString(36).substring(7).includes('x');");
new function
var newFunc = new Function("let r = Math.random().toString(36).substring(7).includes('x');"); newFunc();
regular function
var regularFunc = function() {let r = Math.random().toString(36).substring(7).includes('x');} regularFunc();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Arrow function
eval
new function
regular function
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Arrow function
4271899.5 Ops/sec
eval
2281753.5 Ops/sec
new function
1226249.0 Ops/sec
regular function
4335692.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its various components. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case, which is designed to measure the performance of different approaches for creating small functions or expressions in JavaScript. The test case compares three methods: 1. **Arrow function**: A concise syntax introduced in ECMAScript 2015 (ES6) for defining small, single-purpose functions. 2. **eval**: A built-in function that parses and executes a string as JavaScript code. 3. **Regular function**: A traditional way of defining a function using the `function` keyword. **Options Compared** The three options are compared in terms of their performance, which is measured by the number of executions per second (ExecutionsPerSecond). The benchmark aims to determine which approach provides the best performance for creating and executing small functions or expressions. **Pros and Cons of Each Approach** 1. **Arrow function**: * Pros: Concise syntax, easier to read and write, and often faster execution due to tree shaking optimization. * Cons: Limited support in older browsers (pre-ES6), and some developers may not find it as readable or maintainable. 2. **eval**: * Pros: Supports older JavaScript versions, can be used for dynamic code execution, and is widely supported. * Cons: Slow performance due to parsing and compilation, security risks if not used carefully, and can lead to bloated bytecode. 3. **Regular function**: * Pros: Widely supported in modern browsers, well-established syntax, and often faster execution than eval. * Cons: Longer code length compared to arrow functions, which can be less concise. **Library and Special JS Features** None of the test cases explicitly use any libraries or special JavaScript features. The benchmark focuses solely on comparing the performance of the three methods. **Other Considerations** When writing microbenchmarks like this one, it's essential to consider factors such as: * **Repeatability**: Ensure that the benchmark is repeatable and consistent across different runs. * **System load**: Minimize the impact of system load and other external factors on the benchmark results. * **JavaScript version**: Use a specific JavaScript version or environment to ensure consistency in test results. * **Browser support**: Test on multiple browsers to account for differences in execution performance. **Alternatives** If you're interested in exploring alternative approaches, consider: * **Lambda functions**: Similar to arrow functions but often used in functional programming languages like Haskell or Scala. * **Closures**: A concept that uses a function and its local scope to create a new, isolated context. * **Template literals**: A feature introduced in ES6 for creating string templates with dynamic expressions. Keep in mind that these alternatives may not be directly comparable to the arrow function, eval, and regular function methods in this specific benchmark.
Related benchmarks:
eval vs new Function v3
eval vs new Function #2
eval() vs new Function()
eval vs new Function without cached parsing
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?