Skip to content

Commit 8a5a6ac

Browse files
committed
Improve examples
1 parent 9edaf8f commit 8a5a6ac

File tree

2 files changed

+27
-57
lines changed

2 files changed

+27
-57
lines changed

JavaScript/4-bad.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
11
'use strict';
22

3-
const poolify = (factory, min, norm, max) => {
4-
const duplicate = (n) => new Array(n).fill().map(() => factory());
5-
const items = duplicate(norm);
3+
const poolify = (factory, options, size, max) => {
4+
const instances = [];
5+
for (let i = 0; i < size; i++) {
6+
const instance = factory(...options);
7+
instances.push(instance);
8+
}
69

7-
return (item) => {
8-
if (item) {
9-
if (items.length < max) {
10-
items.push(item);
10+
return (instance) => {
11+
if (instance) {
12+
if (instances.length < max) {
13+
instances.push(instance);
1114
}
12-
console.log('Recycle item, count =', items.length);
13-
return null;
14-
}
15-
if (items.length < min) {
16-
const instances = duplicate(norm - items.length);
17-
items.push(...instances);
1815
}
19-
const res = items.pop();
20-
console.log('Get from pool, count =', items.length);
21-
return res;
16+
instance = instances.pop();
17+
if (!instance) instance = factory(...options);
18+
return instance;
2219
};
2320
};
2421

2522
// Usage
2623

27-
// Factory to allocate 4kb buffer
28-
const buffer = () => new Uint32Array(1024);
29-
30-
// Allocate pool of 10 buffers
31-
const pool = poolify(buffer, 5, 10, 15);
32-
33-
let i = 0;
34-
35-
const next = () => {
36-
const item = pool();
37-
console.log('Buffer size', item.length * 32);
38-
i++;
39-
if (i < 20) {
40-
setTimeout(next, i * 10);
41-
setTimeout(() => pool(item), i * 100);
42-
}
43-
};
24+
const createBuffer = (size) => new Uint8Array(size);
25+
const pool = poolify(createBuffer, [4096], 10, 15);
4426

45-
next();
27+
const instance = pool();
28+
console.log({ instance });
29+
pool(instance);

JavaScript/4-improved.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
'use strict';
22

33
const poolify = (factory, { size, max }) => {
4-
const duplicate = (n) => new Array(n).fill(null).map(factory);
4+
const instances = new Array(size).fill(null).map(factory);
55

6-
const instances = duplicate(size);
7-
8-
const acquire = () => {
9-
const instance = instances.pop() || factory();
10-
console.log('Get from pool, count =', instances.length);
11-
return instance;
12-
};
6+
const acquire = () => instances.pop() || factory();
137

148
const release = (instance) => {
159
if (instances.length < max) {
1610
instances.push(instance);
1711
}
18-
console.log('Recycle item, count =', instances.length);
1912
};
2013

2114
return { acquire, release };
2215
};
2316

2417
// Usage
2518

26-
const buffer = () => new Uint32Array(1024);
19+
const createBuffer = (size) => new Uint8Array(size);
2720

28-
const pool = poolify(buffer, { size: 10, max: 15 });
29-
30-
let i = 0;
31-
const next = () => {
32-
const data = pool.acquire();
33-
console.log('Buffer size', data.length * 32);
34-
i++;
35-
if (i < 20) {
36-
setTimeout(next, i * 10);
37-
setTimeout(() => pool.release(data), i * 100);
38-
}
39-
};
21+
const FILE_BUFFER_SIZE = 4096;
22+
const createFileBuffer = () => createBuffer(FILE_BUFFER_SIZE);
4023

41-
next();
24+
const pool = poolify(createFileBuffer, { size: 10, max: 15 });
25+
const instance = pool.acquire();
26+
console.log({ instance });
27+
pool.release(instance);

0 commit comments

Comments
 (0)