Is calling functions in different JavaScript files slowing down JavaScript processing by the browser?
In other words, can I call functions from a file to another one? Or wou开发者_StackOverflow中文版ld it be better to call function in the same file?
It makes no difference at all. Regardless of which script file the code comes from, it gets intepreted into the document's context, which is the same for all of the script files and inline script on the page.
You can test it with this incredibly sloppy script: http://jsbin.com/uxiye
Somewhat off-topic, but some of the other answerers are absolutely right to point out that while there's no difference executing the functions, there can be a big difference in script load time... You didn't ask about page load time, but still, worth pointing out.
As long as they're all loaded, this is standard functionality and does not result in a performance penalty.
From execution point of view there is no difference.
But remember that loading one all-in-one JS file is faster than many JS files - server needs to be asked only once for first one.
Well, script tags block each other, so they're not all downloading at once. For that you need to do async loading
Secondly, you have to deal with the round-trip calls to the server(s) to get the files and take the latency hit for that.
Lastly, you get to parse and execute code. Assuming you have no duplicate functions they will parse the same and execute the same.
So yes, it's slower, but not because of JavaScript, it's because of HTML and the network. See Steve Souder's site or his book(s) on performance.
精彩评论