First of all, note that this forum is for Computational Science, not Computer Science. These are different fields, with Computational Science being scientific computing, more like computational mathematics and scientific simulations. That said, even though the example in question is not relevant to this forum, there are things that should be discussed here.
The inefficiency is not in the passing of values to functions, but in many other places. This is because a global variable can change at anytime. The program can't know if the value will change, and more importantly in a scripting language, they can change types. Because the program doesn't know if the variable changed, the program has to be ready to deal with whatever has happened. Thus all compiler/interpreter optimizations are turned off and any code around the variables has to go into "safe mode"
Example
A good example is to look at Julia. Julia is a recent programming language which, although it "looks like a scripting language", is actually designed to be efficiently compiled at every step of the way. If you write your Julia code correctly (type-stable, etc.), then your code will compile to be the same speed as C/Fortran (and you can use @code_llvm
to see that the machine code is the same as what you'd get from a C/Fortran compiler). However, the language has problems with global variables. A large discussion on why is found on Github. The summary is as follows:
In a (just-in-time) compiled language like Julia, the compiler looks at what it currently has and uses a bunch of tricks to optimize the code. However, if it doesn't necessarily know what type the variable is, it has to carry around a bunch of code for making it switch between integers/floats/etc. If the variable is a global, it also has to deal with problems like if some other thread changed the value. This means that a lot of other compiler tricks like inlining are also not able to be done. Thus the resulting compiled code has a lot more steps to check every little detail since the global can do just about anything. This makes dealing with them really inefficient.
As you crawl up the ladder to more dynamic typing, these inefficiencies are more baked into the language. At the bottom, all languages are still running compiled code, so something like MATLAB/R/Python always has this extra code around because their numbers can just change types all the time (Javascript does too, though it's odd since "everything is a float" so it has to have parts convert things to integers at the right times). However, smart interpreters (like the Javascript JITs) will sometimes try to still do some optimizations, but for the same reasons as above, they also cannot optimize too much when there are global variables since there's really no way to know in advance what a global variable will be (both in type and value). When all bets are off, they have to play it safe.
(For an example in the other direction, if you pass a variable in and it looks like an integer, and all of the code looks like it won't modify it, it can JIT compile the function so that it actually is an integer, resulting in much leaner and more efficient machine code. Note that this requires that the compiler can guarantee this variable isn't changing anywhere else, otherwise it will cause breakage.)
Conclusion
This is why the general rule for efficiency is to not use global variables. There's no way for the interpreter/compiler to know what to do with them, so they have to take the safest and least efficient route. Also, since they can change anywhere at any time, they can cause a lot of bugs in programs, and so globals are generally frowned upon for design reasons. In the end, unless you really have to, DO NOT USE GLOBALS.
tldr; passing the variable into the function will cause a lot less problems than a global variable.