File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const pool = ( item ) => {
4
+ pool . items = pool . items || new Array ( 10 ) . fill ( new Array ( 1000 ) . fill ( 0 ) ) ;
5
+
6
+ if ( item ) {
7
+ pool . items . push ( item ) ;
8
+ console . log ( 'Recycle item, count =' , pool . items . length ) ;
9
+ return ;
10
+ }
11
+ const res = pool . items . pop ( ) || new Array ( 1000 ) . fill ( 0 ) ;
12
+
13
+ console . log ( 'Get from pool, count =' , pool . items . length ) ;
14
+ return res ;
15
+ } ;
16
+
17
+ // Usage
18
+
19
+ const a1 = pool ( ) ;
20
+ const b1 = a1 . map ( ( x , i ) => i ) . reduce ( ( x , y ) => x + y ) ;
21
+ console . log ( b1 ) ;
22
+
23
+ const a2 = pool ( ) ;
24
+ const b2 = a2 . map ( ( x , i ) => i ) . reduce ( ( x , y ) => x + y ) ;
25
+ console . log ( b2 ) ;
26
+
27
+ pool ( a1 ) ;
28
+ pool ( a2 ) ;
29
+
30
+ const a3 = pool ( ) ;
31
+ const b3 = a3 . map ( ( x , i ) => i ) . reduce ( ( x , y ) => x + y ) ;
32
+ console . log ( b3 ) ;
33
+
34
+ // See: https://github.com/HowProgrammingWorks/Pool
You can’t perform that action at this time.
0 commit comments