I've been looking at other people's JavaScript code, and I've noticed that many programmers tend to create functions that could be combined with the functions that are calling them. One example is this; the 'initWebGL' function could be combined with the 'start' function and it'd function the same. Another example is in the source of this, where function 'tick', which is called every 15 milliseconds, makes calls to two other functions that may just as well be combined with 'tick'. I understand the organizational qualities of this, but I am curious about the effect on performance. Is doing this good practice, especially considering that JavaScript is an interpreted language?
+4
A:
Best practice for any language is to write code that is readable and maintainable first, and then to optimize if needed.
If your program runs fast enough split into easy-to-digest chunks, then leave it that way. If it's running slowly, then like hobodave mentioned, profile to find the cause of the slowness.
Chances are, it's going to be caused by something other than calling functions, but if it happens to be caused by that, then start combining functions together, once you've tracked it down to that.
pib
2010-01-17 09:11:23
+4
A:
http://www.slideshare.net/madrobby/extreme-javascript-performance slides 10..19
10 000 calls makes a difference in IE and Firefox. 1 call don't.
NV
2010-01-17 09:40:01
Whoa, this is very interesting! Thanks a ton!
Skofo
2010-01-17 22:28:10