Skip to content

Commit 6edf98c

Browse files
committed
Object pool
1 parent 2ef21cf commit 6edf98c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

JavaScript/6-pool.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const pool = (item) => {
4+
pool.items = pool.items || new Array(10).fill(new Array(1000).fill(0));
5+
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() || new Array(1000).fill(0);
12+
13+
console.log('Get from pool, count =', pool.items.length);
14+
return res;
15+
};
16+
17+
// Usage
18+
19+
const a1 = pool();
20+
const b1 = a1.map((x, i) => i).reduce((x, y) => x + y);
21+
console.log(b1);
22+
23+
const a2 = pool();
24+
const b2 = a2.map((x, i) => i).reduce((x, y) => x + y);
25+
console.log(b2);
26+
27+
pool(a1);
28+
pool(a2);
29+
30+
const a3 = pool();
31+
const b3 = a3.map((x, i) => i).reduce((x, y) => x + y);
32+
console.log(b3);
33+
34+
// See: https://github.com/HowProgrammingWorks/Pool

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