Skip to content

Instantly share code, notes, and snippets.

@prafulliu
Created December 6, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prafulliu/fc252a8dabfc2632159a to your computer and use it in GitHub Desktop.
Save prafulliu/fc252a8dabfc2632159a to your computer and use it in GitHub Desktop.
'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