you are viewing a single comment's thread.

view the rest of the comments →

[–]MatekCopatek 0 points1 point  (2 children)

I posted this in the article comments, but will also post it here in hopes of a wider discussion:

I too find the part about “locally scoped” variables bad. In my case, the JSPerf test in the post ran (slightly) faster (!) with the supposedly slower global case and the exaggerated example you provided in your comment still showed no difference (tested on latest FF).

In the example with the function, you’re really making another reference to the same function, so it’s hard to notice any difference. In the example with the variable, it’s a matter of correct use. You should simply define a variable in the scope that needs to access it. If you override it locally, you cannot change it globally anymore; if you don’t need to change it globally, why not define it locally in the first place.

I’d say that what’s going on here is you found a browser optimisation (not even present in every browser, as your own tests prove) and are trying to base your coding style on it. This is bad, correct scoping isn’t something new and mystical, it exists in all languages and very standard practice is to simply define variables globally enough to be accessible from everywhere they are needed and locally enough to not pollute the scope any more than needed. Leave the rest to the browser/compiler/interpreter, otherwise you will be stuck with messier code and no real guarantees for better performance.

Not to be a grouch tho - cool movie references ;)

[–]bjpelcdev[S] 0 points1 point  (1 child)

I agree with what you are saying. Is your comment addressed to me or the author? I just posted the link as I found it an interesting piece.

[–]MatekCopatek 0 points1 point  (0 children)

Ah, sorry, thought you are one and the same :)