File tree Expand file tree Collapse file tree 2 files changed +17
-6
lines changed
Expand file tree Collapse file tree 2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
22
3+ // TODO: Refactor to respect SoC principle and
4+ // optimize JavaScript for V8
5+
36const poolify = ( factory , options , size , max ) => {
4- const instances = [ ] ;
5- for ( let i = 0 ; i < size ; i ++ ) {
6- const instance = factory ( ...options ) ;
7+ const instances = [ ] ; // Preallocate array
8+ for ( let i = 0 ; i < size ; i ++ ) { // Use Array methods instead of loop
9+ const instance = factory ( ...options ) ; // Avoid array destructuring
710 instances . push ( instance ) ;
811 }
912
10- return ( instance ) => {
11- if ( instance ) {
13+ return ( instance ) => { // Respect SoC and SOLID/SRP
14+ if ( instance ) { // Avoid if-statement
1215 if ( instances . length < max ) {
1316 instances . push ( instance ) ;
1417 }
1518 }
16- instance = instances . pop ( ) ;
19+ instance = instances . pop ( ) ; // Do not reassign incoming parameters
1720 if ( ! instance ) instance = factory ( ...options ) ;
1821 return instance ;
1922 } ;
Original file line number Diff line number Diff line change 1+ # Pool tasks
2+
3+ 1 . Optimize ` 4-bad.js ` , see TODOs in the file; check solution if ` 4-improved.js `
4+ 2 . Rewrite ` 4-improved.js ` to class syntax
5+ 3 . Implement async instance acquisition in closure version with queue and callbacks
6+ 4 . Implement async instance acquisition in classes version with queue and callbacks
7+ 5 . Rewrite acquisition to promises in closure version
8+ 6 . Rewrite acquisition to promises in classes version
You can’t perform that action at this time.
0 commit comments