lib: refactor internal/freelist
PR-URL: https://github.com/nodejs/node/pull/11406 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
d61a511728
commit
f04387e9f2
@ -1,24 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
// This is a free list to avoid creating so many of the same object.
|
||||
exports.FreeList = function(name, max, constructor) {
|
||||
this.name = name;
|
||||
this.constructor = constructor;
|
||||
this.max = max;
|
||||
this.list = [];
|
||||
};
|
||||
|
||||
|
||||
exports.FreeList.prototype.alloc = function() {
|
||||
return this.list.length ? this.list.pop() :
|
||||
this.constructor.apply(this, arguments);
|
||||
};
|
||||
|
||||
|
||||
exports.FreeList.prototype.free = function(obj) {
|
||||
if (this.list.length < this.max) {
|
||||
this.list.push(obj);
|
||||
return true;
|
||||
class FreeList {
|
||||
constructor(name, max, ctor) {
|
||||
this.name = name;
|
||||
this.ctor = ctor;
|
||||
this.max = max;
|
||||
this.list = [];
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
alloc() {
|
||||
return this.list.length ? this.list.pop() :
|
||||
this.ctor.apply(this, arguments);
|
||||
}
|
||||
|
||||
free(obj) {
|
||||
if (this.list.length < this.max) {
|
||||
this.list.push(obj);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {FreeList};
|
||||
|
Loading…
x
Reference in New Issue
Block a user