Last active
September 14, 2021 14:46
-
-
Save louisstow/5609992 to your computer and use it in GitHub Desktop.
Generic object pool in JavaScript
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
var objectPool = []; | |
var marker = 0; | |
var poolSize = 0; | |
//any old JavaScript object | |
function commonObject () { } | |
commonObject.create = function () { | |
if (marker >= poolSize) { | |
commonObject.expandPool(poolSize * 2); | |
} | |
var obj = objectPool[marker++]; | |
obj.index = marker - 1; | |
obj.constructor.apply(obj, arguments); | |
return obj; | |
} | |
//push new objects onto the pool | |
commonObject.expandPool = function (newSize) { | |
for (var i = 0; i < newSize - poolSize; ++i) { | |
objectPool.push(new commonObject()); | |
} | |
poolSize = newSize; | |
} | |
//swap it with the last available object | |
commonObject.prototype.destroy = function () { | |
marker--; | |
var end = objectPool[marker]; | |
var endIndex = end.index; | |
objectPool[marker] = this; | |
objectPool[this.index] = end; | |
end.index = this.index; | |
this.index = endIndex; | |
} | |
//make this as big as you think you need | |
commonObject.expandPool(1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
commonObject
with the name of your JavaScript class.commonObject.create(...)
. You can pass any data as if it was the constructor..destroy()
method on the instance.