开发者

How expensive are function calls in JavaScript?

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 c开发者_运维问答ombined 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?


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.


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.


The best way to learn how "expensive function calls are", a completely vague and context specific metric, is to profile it yourself:

  • Download Firefox
  • Download Firebug
  • Profile with firebug
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜