mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
c647e87504
Array#pop() is known to be faster than Array#shift(). To be exact, it's O(1) vs. O(n). In this case there's no difference from which side of the "pool" array the object is retrieved, so .pop() should be preferred. PR-URL: https://github.com/nodejs/node/pull/2174 Reviewed-By: mscdex - Brian White <mscdex@mscdex.net> Reviewed-By: jasnell - James M Snell <jasnell@gmail.com> Reviewed-By: ofrobots - Ali Ijaz Sheikh <ofrobots@google.com>
25 lines
553 B
JavaScript
25 lines
553 B
JavaScript
'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;
|
|
}
|
|
return false;
|
|
};
|