vendredi 26 décembre 2014

How to handle variables in loops from functions and functions in regards to garbage collector?


I'm trying to create my little particle system as a self study to learn more about javascript. Althought I'm not a complete beginner to programming I'm anything but professional programmer. Currently I'm trying to advance my knowledge of pure javascript (thus no libraries in my code).


After reading an article by google, and many others, I found myself perplexed because of garbage collection. According to the article, memory chart, compared to my program's, should look more smoother and GC shouldn't be called that often too. Below is link to screenshot from google chromes developer tools.


There are two things I'd like to ask you and I'd appreciate any insight from you.


a/ Could this kind of behaviour be possibly the result of calling var inside functions for loop indexing?


Example:



function doSomething(args) {
for (var i = 0; i < arr.length; i++) {/*some code*/}
}


My animation loop calls different functions very often (requestanimationframe). How do you deal with these variables which are used by loops (var i)? Declaring them outside as globals? But that gets quite ugly if you get nested loops from different function calls.


b/ Does assigning functions to variables influences memory stacking (creating new functions on run)?


In an article I found that assigning function to variable prevents creating new functions on every function call. I tried assigning all my functions to variables and calling them this way but sadly it didn't change anything (performance, memory chart).


Fiddle (linke below) of my whole current little particle system with a test scene. There is about 8700 particles on screen, with my super slow processor it runs at constant 32 fps. With quad-core processor I get 54-60 fps depending on settings (60 fps if I don't draw still objects as generators, attractors, ...).


But I periodicaly get slight choppiness when I run it so I'd like to be more effecient with my code and tweak it. But methods I found didn't work for me. Luckily since the beginning I was recycling particle objects so after getting to above mentioned 8700 particles I almost don't create any new objects with new Constructor.


So I'd like to kindly ask for your advice.


Thank you very much for reading this far :-).


Other links:


fiddle: http://jsfiddle.net/kwgp7kmh/1/


Memory chart from chrome dev tools: http://postimg.org/image/wl7pm4zzp/





Aucun commentaire:

Enregistrer un commentaire