Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
factory function vs function
(version: 0)
Comparing performance of:
factory call vs function call vs factory init vs function init
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function factory(){ return ()=>{ const a = "A" return a; } }
Tests:
factory call
const foo = factory(); function bar (){ const a = "A" return a; } foo(); foo(); foo(); foo(); foo(); foo();
function call
const foo = factory(); function bar (){ const a = "A" return a; } bar(); bar(); bar(); bar(); bar(); bar();
factory init
const fooA = factory(); const fooB = factory(); const fooC = factory(); const fooD = factory(); const fooE = factory(); const fooF = factory();
function init
function barA (){ const a = "A" return a; } function barB (){ const a = "A" return a; } function barC (){ const a = "A" return a; } function barD (){ const a = "A" return a; } function barE (){ const a = "A" return a; } function barF (){ const a = "A" return a; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
factory call
function call
factory init
function init
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
factory call
177862784.0 Ops/sec
function call
150422208.0 Ops/sec
factory init
153218192.0 Ops/sec
function init
163807744.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three different approaches to create functions in JavaScript: 1. **Factory Function**: A function that returns another function, often used for creating reusable code snippets. 2. **Function Call**: Creating a new function instance by calling an existing function. 3. **Function Initialization**: Creating multiple function instances using the same function declaration. **Test Cases** There are four test cases in this benchmark: 1. **Factory Call**: Calls the `factory` function, which returns another function, and then calls that returned function seven times. 2. **Function Call**: Calls an existing `bar` function seven times directly. 3. **Factory Init**: Creates six instances of the `factory` function, which return different functions, and then calls each instance. 4. **Function Init**: Creates five instances of the `bar` function and calls each one. **Library Used** There is no explicit library used in this benchmark, but it's worth noting that some JavaScript engines may provide optimizations or features that affect the performance of these tests. For example, some engines might optimize constant expressions or handle function calls differently. **Special JavaScript Feature/Syntax** None mentioned explicitly in the provided code snippet. However, keep in mind that some JavaScript features like `let` and `const` declarations, arrow functions, and template literals may impact the results. **Comparison of Approaches** Here's a brief overview of the pros and cons of each approach: * **Factory Function**: Pros: Reusable code snippets, easier maintenance. Cons: Potential performance overhead due to function creation and call. * **Function Call**: Pros: Simple implementation, often used in existing codebases. Cons: Can lead to slower performance compared to factory functions, especially for repeated calls. * **Function Initialization**: Pros: Can be faster than factory functions for simple implementations. Cons: May not be as reusable or maintainable. **Benchmark Results** The latest benchmark results show that the `function init` approach is the fastest, followed by the `factory call` and then the `factory function`. The `function call` approach is the slowest. **Alternative Approaches** Other alternatives to consider: * **Closure**: Instead of using a factory function or function call, you could use closures to encapsulate variables and functions. This approach can be more efficient than creating new functions. * **Generator Functions**: You could use generator functions instead of factory functions, which can provide better performance for certain use cases. Keep in mind that the best approach depends on your specific requirements, performance needs, and coding style preferences.
Related benchmarks:
Arrow function vs function comparison
Re-used function declaration vs Creating arrow function from factory vs Creating plain function from factory
Object creation vs function definition vs arror function vs function expression vs named function expression
Const vs Function
Comments
Confirm delete:
Do you really want to delete benchmark?