Skip to content

Commit

Permalink
lib: refactor internal/freelist
Browse files Browse the repository at this point in the history
PR-URL: #11406
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jasnell committed Feb 22, 2017
1 parent d61a511 commit f04387e
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/internal/freelist.js
Original file line number Diff line number Diff line change
@@ -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);
};
class FreeList {
constructor(name, max, ctor) {
this.name = name;
this.ctor = ctor;
this.max = max;
this.list = [];
}

alloc() {
return this.list.length ? this.list.pop() :
this.ctor.apply(this, arguments);
}

exports.FreeList.prototype.free = function(obj) {
if (this.list.length < this.max) {
this.list.push(obj);
return true;
free(obj) {
if (this.list.length < this.max) {
this.list.push(obj);
return true;
}
return false;
}
return false;
};
}

module.exports = {FreeList};

0 comments on commit f04387e

Please sign in to comment.