nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
27 lines (22 loc) • 499 B
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/freelist.js
class FreeList {
constructor(name, max, ctor) {
this.name = name;
this.ctor = ctor;
this.max = max;
this.list = [];
}
alloc() {
return this.list.length > 0
? this.list.pop()
: ReflectApply(this.ctor, this, arguments);
}
free(obj) {
if (this.list.length < this.max) {
this.list.push(obj);
return true;
}
return false;
}
}
export default FreeList;