I’m improving the performance of a HTML5 Game,while I reset some arrays,in order to reduce the garbage collection,I tend to use array.length=0(Yet I’m not sure if it’s working in real world)
I did some tests to find out the speed of these 2 types of reset array here
http://tinkerbin.com/hqQvp5fQ
I’m improving the performance of a HTML5 Game,while I reset some arrays,in order to
Share
Yes, setting
lengthto zero is a better solution since it truncates an existing array whilearr=[]creates a new array and stores a reference to it inarr. It may happen that the old array isn’t referenced any more and will be garbage collected. Hence,arr=[]may require an extra allocation and garbage collection compared toarr.length=0.See this for the description of the special behavior of the
lengthproperty on which this relies.