Created
September 3, 2013 11:49
-
-
Save Sjeiti/6422815 to your computer and use it in GitHub Desktop.
Simple object pool
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
if (!window.myObj) { | |
myObj = (function(){ | |
var aPool = []; | |
function obj(x,y) { | |
var fX,fY | |
,oObj = { | |
set:set | |
,drop:drop | |
} | |
; | |
set(x,y); | |
function set(x,y) { | |
fX = x===undefined?1:x; | |
fY = y===undefined?0:y; | |
return oObj; | |
} | |
function drop() { | |
aPool.push(oObj); | |
} | |
return oObj; | |
} | |
return function(x,y) { | |
return aPool.length?aPool.pop().set(x,y):obj(x,y); | |
}; | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment