Explorer
Node.js

Implement a `createPool(factory, maxSize)` that reuses objects. `acquire()` gets an object, `release(obj)` returns it to the pool.

Problem Statement

Implement a `createPool(factory, maxSize)` that reuses objects. `acquire()` gets an object, `release(obj)` returns it to the pool.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: An object pool avoids expensive creation by reusing instances. šŸ’” Hint 2: Maintain a list of available objects. On acquire, take one; on release, put it back. šŸ’” Hint 3: If pool is empty on acquire, create a new object with factory(). āœ… Solution: ```javascript function createPool(factory, maxSize) { const available = []; let created = 0; return { acquire() { if (available.length > 0) return available.pop(); if (created < maxSize) { created++; return factory(); } throw new Error("Pool exhausted"); }, release(obj) { available.push(obj); }, size() { return { available: available.length, created }; } }; } ```

Object Pool Pattern

Hard
Implement a `createPool(factory, maxSize)` that reuses objects. `acquire()` gets an object, `release(obj)` returns it to the pool.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"aId":1,"bId":2,"cId":1,"sameAsA":true}