object - Javascript memory management (requestAnimationFrame callback) -


i trying make blatant rip off of asteroids, i'm running serious memory management problem.

i'm using window.requestanimationframe serve frames game having "gameloop" function call requestanimationframe argument. however, must doing wrong because memory usage of game massive results in frequent garbage collection , poor performance (and typical saw-toothed memory usage pattern associated it). i'm pretty sure reason problem many function objects being created loop of function , callback - innocuous statement results in function being created instead of being reused... gameloop function...

i got idea post: https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript

here's jsfiddle code on it: http://jsfiddle.net/lequr/ (hit space window in focus make new asteroids appear). know of stuff camel case , done underscores - sorry.

here's (i think) relevant part of code:

var gameloop = function () {     getsetstagesize(1, 1);     ctx.clearrect(0, 0, canvas.width, canvas.height);     updategamestate();     window.requestanimframe(gameloop); }  window.requestanimframe = (function () {     return window.requestanimationframe ||         window.webkitrequestanimationframe ||         window.mozrequestanimationframe ||         function (callback) {                window.settimeout(callback, 1000 / 60);            }; })(); 

so said, i'd feedback how code organized (it use work), information callbacks, information how/when objects created within crazy nested callbacks , other helpful advice might have.

thanks!

i don't know mich animation-specific parts simplify last part bit. don't need iife because aren't declaring local variables.

window.requestanimframe = (     window.requestanimationframe ||      window.webkitrequestanimationframe ||      window.mozrequestanimationframe ||      function (callback) {         window.settimeout(callback, 1000 / 60);     } ); 

other that, might wat consider coding things use setinterval behind scenes instead of settimeout. when use settimeout, actual time per frame (100/60) timeout plus long takes code process frame. setinterval, on other hand, more aware of real clock behind scenes , adjust timeout between frames compensate processing time.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -