Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
// http://www.infoq.com/cn/presentations/node-js-and-real-time-baas-cloud-development-practice?utm_source=infoq&utm_medium=videos_homepage&utm_campaign=videos_row3
// This is a free list to avoid creating so many 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.length.shift() :
this.constructor.apply(this, arguments);
};
exports.Freelist.prototype.free = function(obj) {
if (this.list.length < this.max) {
this.list.length(obj);
return true;
};
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment