Last active
January 12, 2016 19:18
-
-
Save tehsquidge/d0e419ade698c7e483e2 to your computer and use it in GitHub Desktop.
This file contains 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
function ObjectPool(classType, growthVector, resetFunction){ | |
this._freePool = []; | |
this._activePool = []; | |
this._classType = classType; | |
this._growthVector = growthVector; | |
this._reset = resetFunction; | |
} | |
ObjectPool.prototype = Object.create(null, { | |
constructor: { | |
value: ObjectPool | |
}, | |
construct: { | |
value: function(constructor, args) { | |
function F() { | |
return constructor.apply(this, args); | |
} | |
F.prototype = constructor.prototype; | |
return new F(); | |
}, | |
enumerable: false, //CG hide from devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
}, | |
acquire: { | |
value: function(){ | |
var obj; | |
if(this._freePool.length < 1){ | |
obj = this.construct(this._classType, arguments); | |
this._activePool.push(obj); | |
for(var i = 1; i < this._growthVector * this.poolSize(); i++ ){ | |
var dudObj = this.construct(this._classType, null); | |
this._reset(dudObj); | |
this._freePool.push(dudObj); | |
} | |
}else{ | |
obj = this._freePool.pop(); | |
this._classType.apply(obj,arguments); | |
this._activePool.push(obj); | |
} | |
return obj; | |
}, | |
enumerable: true, //CG show to devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
}, | |
release: { | |
value: function(obj){ | |
var i = this._activePool.indexOf(obj); | |
this._reset(obj); | |
delete this._activePool[i]; | |
this._freePool.push(obj); | |
}, | |
enumerable: true, //CG show to devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
}, | |
poolSize: { | |
value: function(){ | |
return (this._freePool.length + this._activePool.length); | |
}, | |
enumerable: true, //CG show to devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
}, | |
activePoolSize: { | |
value: function(){ | |
return this._activePool.length; | |
}, | |
enumerable: true, //CG show to devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
}, | |
freePoolSize: { | |
value: function(){ | |
return this._freePool.length; | |
}, | |
enumerable: true, //CG show to devs | |
writable: false, //CG readonly | |
configurable: false //CG can not change above | |
} | |
}); | |
function Person(name){ | |
this._name = name; | |
} | |
Person.prototype = Object.create(Object, { | |
constructor: { | |
value: Person | |
}, | |
name: { | |
get: function(){ | |
return this._name; | |
}, | |
set: function(name){ | |
this._name = name; | |
} | |
} | |
}); | |
var pool = new ObjectPool(Person,0.5,function(p){ | |
p.name = ""; | |
}); | |
var p = pool.acquire('bob'); | |
console.log(p.name); | |
var p2 = pool.acquire('jane'); | |
console.log(p2.name); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment