Skip to content

Commit 984e4d7

Browse files
committed
Improve examples
1 parent 97cbd8f commit 984e4d7

File tree

10 files changed

+257
-258
lines changed

10 files changed

+257
-258
lines changed

JavaScript/0-factory.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
'use strict';
22

3-
const pool = (factory) => {
3+
const poolify = (factory) => {
44
const instances = new Array(5).fill(null).map(() => factory.create());
5-
return (instance) => {
6-
if (instance) {
7-
instances.push(instance);
8-
console.log('Recycle item, count =', instances.length);
9-
return null;
10-
}
11-
const result = instances.pop();
5+
6+
const acquire = () => {
7+
const instance = instances.pop();
128
console.log('Get from pool, count =', instances.length);
13-
return result;
9+
return instance;
10+
};
11+
12+
const release = (instance) => {
13+
instances.push(instance);
14+
console.log('Recycle item, count =', instances.length);
1415
};
16+
17+
return { acquire, release };
1518
};
1619

1720
class Connection {
@@ -20,7 +23,7 @@ class Connection {
2023
}
2124
}
2225

23-
class Factory {
26+
class ConnectionFactory {
2427
constructor() {
2528
this.index = 0;
2629
}
@@ -30,18 +33,10 @@ class Factory {
3033
}
3134
}
3235

33-
/*
34-
35-
const alternativeFactory = (() => {
36-
let index = 0;
37-
return () => new Connection(`http://10.0.0.1/${index++}`);
38-
})();
39-
40-
*/
41-
4236
// Usage
4337

44-
const connectionFactory = new Factory();
45-
const connectionPool = pool(connectionFactory);
46-
const connection = connectionPool();
38+
const factory = new ConnectionFactory();
39+
const pool = poolify(factory);
40+
const connection = pool.acquire();
4741
console.log(connection);
42+
pool.release(connection);

JavaScript/1-simple.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@ class Connection {
1212
}
1313
}
1414

15-
const pool = () => {
16-
const instances = new Array(5).fill(null).map(Connection.create);
17-
return (instance) => {
18-
if (instance) {
19-
instances.push(instance);
20-
console.log('Recycle item, count =', instances.length);
21-
return null;
22-
}
23-
const result = instances.pop();
15+
const POOL_SIZE = 5;
16+
17+
const poolify = (factory) => {
18+
const instances = new Array(POOL_SIZE).fill(null).map(factory);
19+
20+
const acquire = () => {
21+
const instance = instances.pop();
2422
console.log('Get from pool, count =', instances.length);
25-
return result;
23+
return instance;
2624
};
25+
26+
const release = (instance) => {
27+
instances.push(instance);
28+
console.log('Recycle item, count =', instances.length);
29+
};
30+
31+
return { acquire, release };
2732
};
2833

2934
// Usage
3035

31-
const connectionPool = pool(Connection.create);
36+
const pool = poolify(Connection.create);
3237

3338
for (let i = 0; i < 7; i++) {
34-
const connection = connectionPool();
39+
const connection = pool.acquire();
3540
console.log(connection);
3641
}

JavaScript/2-drain.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
'use strict';
22

3-
const pool = (factory) => {
4-
const instances = new Array(5).fill(null).map(factory);
5-
return (instance) => {
6-
if (instance) {
7-
instances.push(instance);
8-
console.log('Recycle item, count =', instances.length);
9-
return null;
10-
}
11-
const result = instances.pop() || factory();
3+
const POOL_SIZE = 5;
4+
5+
const poolify = (factory) => {
6+
const instances = new Array(POOL_SIZE).fill(null).map(factory);
7+
8+
const acquire = () => {
9+
const instance = instances.pop() || factory();
1210
console.log('Get from pool, count =', instances.length);
13-
return result;
11+
return instance;
1412
};
15-
};
1613

17-
// Usage
14+
const release = (instance) => {
15+
instances.push(instance);
16+
console.log('Recycle item, count =', instances.length);
17+
};
18+
19+
return { acquire, release };
20+
};
1821

1922
class Connection {
2023
static index = 0;
@@ -28,9 +31,11 @@ class Connection {
2831
}
2932
}
3033

31-
const connectionPool = pool(Connection.create);
34+
// Usage
35+
36+
const pool = poolify(Connection.create);
3237

3338
for (let i = 0; i < 10; i++) {
34-
const connection = connectionPool();
39+
const connection = pool.acquire();
3540
console.log(connection);
3641
}

JavaScript/3-poolify.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

JavaScript/3-size.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const poolify = (factory, size) => {
4+
const instances = new Array(size).fill(null).map(factory);
5+
6+
const acquire = () => {
7+
const instance = instances.pop() || factory();
8+
console.log('Get from pool, count =', instances.length);
9+
return instance;
10+
};
11+
12+
const release = (instance) => {
13+
instances.push(instance);
14+
console.log('Recycle item, count =', instances.length);
15+
};
16+
17+
return { acquire, release };
18+
};
19+
20+
// Usage
21+
22+
// Factory to allocate 32kb buffer
23+
const buffer = () => new Uint32Array(1024);
24+
25+
// Allocate pool of 10 buffers
26+
const pool = poolify(buffer, 10);
27+
28+
for (let i = 0; i < 15; i++) {
29+
const data = pool.acquire();
30+
console.log('Buffer size', data.length * 32);
31+
}

JavaScript/4-improved.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
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);
6-
7-
return (item) => {
8-
if (item) {
9-
if (items.length < max) {
10-
items.push(item);
11-
}
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);
3+
const poolify = (factory, { size, max }) => {
4+
const duplicate = (n) => new Array(n).fill(null).map(factory);
5+
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+
};
13+
14+
const release = (instance) => {
15+
if (instances.length < max) {
16+
instances.push(instance);
1817
}
19-
const res = items.pop();
20-
console.log('Get from pool, count =', items.length);
21-
return res;
18+
console.log('Recycle item, count =', instances.length);
2219
};
20+
21+
return { acquire, release };
2322
};
2423

2524
// Usage
2625

27-
// Factory to allocate 4kb buffer
2826
const buffer = () => new Uint32Array(1024);
2927

30-
// Allocate pool of 10 buffers
31-
const pool = poolify(buffer, 5, 10, 15);
28+
const pool = poolify(buffer, { size: 10, max: 15 });
3229

3330
let i = 0;
34-
3531
const next = () => {
36-
const item = pool();
37-
console.log('Buffer size', item.length * 32);
32+
const data = pool.acquire();
33+
console.log('Buffer size', data.length * 32);
3834
i++;
3935
if (i < 20) {
4036
setTimeout(next, i * 10);
41-
setTimeout(() => pool(item), i * 100);
37+
setTimeout(() => pool.release(data), i * 100);
4238
}
4339
};
4440

JavaScript/5-async.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
11
'use strict';
22

3-
const duplicate = (factory, n) => new Array(n).fill().map(() => factory());
3+
const duplicate = (factory, n) => new Array(n).fill(null).map(factory);
44

5-
const poolify = (factory, min, norm, max) => {
6-
let allocated = norm;
7-
const items = duplicate(factory, norm);
5+
const poolify = (factory, { size, max, step }) => {
6+
let allocated = size;
7+
const instances = duplicate(factory, size);
88
const delayed = [];
99

10-
return (par) => {
11-
if (typeof par !== 'function') {
12-
if (items.length < max) {
13-
const request = delayed.shift();
14-
if (request) {
15-
const c1 = items.length;
16-
console.log(`${c1}->${c1} Recycle item, pass to delayed`);
17-
request(par);
18-
} else {
19-
const c1 = items.length;
20-
items.push(par);
21-
const c2 = items.length;
22-
console.log(`${c1}->${c2} Recycle item, add to pool`);
23-
}
24-
}
25-
return;
26-
}
27-
if (items.length < min && allocated < max) {
28-
const grow = Math.min(max - allocated, norm - items.length);
10+
const acquire = (callback) => {
11+
const c1 = instances.length;
12+
if (instances.length === 0 && allocated < max) {
13+
const grow = Math.min(max - allocated, size - instances.length);
2914
allocated += grow;
30-
const instances = duplicate(factory, grow);
31-
items.push(...instances);
15+
console.log({ grow });
16+
const addition = duplicate(factory, grow);
17+
instances.push(...addition);
3218
}
33-
const c1 = items.length;
34-
const res = items.pop();
35-
const c2 = items.length;
36-
if (res) {
37-
console.log(`${c1}->${c2} Get from pool, pass to callback`);
38-
par(res);
19+
const instance = instances.pop();
20+
const c2 = instances.length;
21+
const delta = `Get from pool: ${c1}->${c2} of ${allocated}`;
22+
if (instance) {
23+
console.log(`${delta}, pass to callback`);
24+
callback(instance);
3925
} else {
40-
console.log(`${c1}->${c2} Get from pool, add callback to queue`);
41-
delayed.push(par);
26+
console.log(`${delta}, add callback to queue`);
27+
delayed.push(callback);
4228
}
4329
};
30+
31+
const release = (instance) => {
32+
const c1 = instances.length;
33+
if (delayed.length > 0) {
34+
const request = delayed.shift();
35+
const delta = `Recycle item: ${c1}->${c1} of ${allocated}`;
36+
console.log(`${delta}, pass to delayed`);
37+
request(instance);
38+
} else {
39+
if (instances.length < max) {
40+
instances.push(instance);
41+
}
42+
const c2 = instances.length;
43+
const delta = `Recycle item: ${c1}->${c2} of ${allocated}`;
44+
console.log(`${delta}, add to pool`);
45+
}
46+
};
47+
48+
return { acquire, release };
4449
};
4550

4651
// Usage
4752

48-
// Factory to allocate 4kb buffer
4953
const buffer = () => new Uint32Array(1024);
5054

51-
// Allocate pool of 10 buffers
52-
const pool = poolify(buffer, 3, 5, 7);
55+
const pool = poolify(buffer, { size: 5, max: 7, step: 3 });
5356

5457
let i = 0;
55-
5658
const next = () => {
57-
pool((item) => {
59+
pool.acquire((data) => {
5860
i++;
5961
if (i < 20) {
6062
setTimeout(next, i * 10);
61-
setTimeout(() => pool(item), i * 100);
63+
setTimeout(() => pool.release(data), i * 100);
6264
}
6365
});
6466
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy