Created
December 6, 2015 16:24
-
-
Save prafulliu/fc252a8dabfc2632159a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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