We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 689c5f0 commit 5cb5c81Copy full SHA for 5cb5c81
1 file changed
JavaScript/3-poolify.js
@@ -0,0 +1,33 @@
1
+'use strict';
2
+
3
+const poolify = (factory, size) => {
4
5
+ const pool = (item) => {
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() || factory();
12
13
+ console.log('Get from pool, count =', pool.items.length);
14
+ return res;
15
+ };
16
17
+ const items = new Array(size).fill().map(() => factory());
18
+ return Object.assign(pool, { items });
19
20
+};
21
22
+// Usage
23
24
+// Factory to allocate 4kb buffer
25
+const buffer = () => new Uint32Array(128);
26
27
+// Allocate pool of 10 buffers
28
+const pool = poolify(buffer, 10);
29
30
+for (let i = 0; i < 15; i++) {
31
+ const a = pool();
32
+ console.log('Buffer size', a.length * 32);
33
+}
0 commit comments