-
Notifications
You must be signed in to change notification settings - Fork 10
/
4-improved.js
45 lines (37 loc) · 964 Bytes
/
4-improved.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const poolify = (factory, min, norm, max) => {
const duplicate = (n) => new Array(n).fill().map(() => factory());
const items = duplicate(norm);
return (item) => {
if (item) {
if (items.length < max) {
items.push(item);
}
console.log('Recycle item, count =', items.length);
return null;
}
if (items.length < min) {
const instances = duplicate(norm - items.length);
items.push(...instances);
}
const res = items.pop();
console.log('Get from pool, count =', items.length);
return res;
};
};
// Usage
// Factory to allocate 4kb buffer
const buffer = () => new Uint32Array(1024);
// Allocate pool of 10 buffers
const pool = poolify(buffer, 5, 10, 15);
let i = 0;
const next = () => {
const item = pool();
console.log('Buffer size', item.length * 32);
i++;
if (i < 20) {
setTimeout(next, i * 10);
setTimeout(() => pool(item), i * 100);
}
};
next();