Skip to content

Commit 9d346de

Browse files
committed
Example with 32kb buffers
1 parent 620f44b commit 9d346de

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

JavaScript/7-buffer.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const poolify = (factory, min, norm, max) => {
4+
const duplicate = n => new Array(n).fill().map(() => factory());
5+
6+
const pool = (item) => {
7+
if (item) {
8+
if (pool.allocated <= max) {
9+
pool.items.push(item);
10+
} else {
11+
pool.allocated--;
12+
}
13+
console.log('Recycle item, count =', pool.items.length, pool.allocated);
14+
return;
15+
}
16+
if (pool.items.length < min) {
17+
const items = duplicate(norm - pool.items.length);
18+
pool.allocated += pool.items.length;
19+
pool.items.push(...items);
20+
}
21+
const res = pool.items.pop();
22+
23+
console.log('Get item, count =', pool.items.length, pool.allocated);
24+
return res;
25+
};
26+
27+
const items = duplicate(norm);
28+
return Object.assign(pool, { items, allocated: norm });
29+
};
30+
31+
const buffer = () => new Uint32Array(1024);
32+
33+
const pool = poolify(buffer, 5, 10, 13);
34+
35+
let i = 0;
36+
37+
const next = () => {
38+
const item = pool();
39+
console.log('Buffer size', item.length * 32);
40+
i++;
41+
if (i < 20) {
42+
setTimeout(next, i * 10);
43+
setTimeout(() => pool(item), i * 100);
44+
}
45+
};
46+
47+
next();

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