1

In a recent question (Using a for loop to condense code) on stack overflow, I've learned that you can loop through variable names via the window object.

After reading this I was wondering if this is considered bad habbit and if this affects performance in certain ways.

I would guess that this does lower the performance of you Javascript since you are creating a lot more variables than when you are using an array for example. but if you are accessing a variable via it's direct name "variable" vs. "window[variable]", does this have a certain impact as well? Is this a different approach for retreiving the value? Or are these 2 possibilities doing exactly the same thing?


EDIT: As @Tomalak said, it's probably useless to ask for performance problems, since I'm not experiencing them.

Since it is a bad habbit to put a lot of variable names in the window object, what would be a use case where you would use window[variable] instead of an alternative? Is there a usecase for this?

11
  • 12
    A first best practice would be not to put variables in the window (global) scope... Commented Oct 30, 2013 at 14:04
  • 5
    Whatever the performance implications are here, it's plain and simple bad practice, because variable variables are virtually always bad practice! For dynamic lists of an unknown number of items you use arrays, that's what they're there for. Commented Oct 30, 2013 at 14:05
  • 8
    Don't ask performance questions unless you have actual, specific performance problems. You don't need to optimize for problems you don't have and you should not waste your time thinking into this direction. Whether it's a good thing to put many variables into the window object and what alternatives exist that's the real question here. Commented Oct 30, 2013 at 14:06
  • 1
    It is, as others had said, bad practice. However, for performance questions, check out jsperf and you can answer these questions yourself. Commented Oct 30, 2013 at 14:06
  • 2
    If you really want to use this pattern, use your own object instead of window so you don't pollute the global namespace. Commented Oct 30, 2013 at 14:09

3 Answers 3

1

It's generally a bad idea to use global variables. It's also considered bad practice to have dynamic variable names, which is what you would be doing when using this technique. To store lists of items, use an array. That's what arrays are there for.

Now, as for computing time... the difference is on a scale of fractions of fractions of seconds. However, using window["variable" + i] is slower because in order to look up the appropriate value, the property string needs to be concatenated. With an array, you just pass in the index, and nothing needs to be concatenated or converted to a string, which is what happens with object lookups. So avoiding window["variable" + i] is actually minutely faster.

As for cases when using dynamic variables is actually useful--well, there aren't many. The only possible use for window[variableName] that I can think of is if variableName refers to a function you need to call. That would look like this: window["someFunction"]();.

tl;dr: Don't do this, as it affects performance negatively (albeit negligibly) and is bad practice on several levels. The only time it's "okay" is when you're calling a function by name--but be careful there, too, as doing that is pretty much equivalent to using eval, which is, of course, evil.

Sign up to request clarification or add additional context in comments.

Comments

1

window object is already full of variables and despite tons of smartheads in internet trying to dictate the universally best coding style, it is still mostly matter of personal preference. One has to do what one has to do. In short, @Tomalak nailed it with his comment - there's no need to over-engineer it. Simplicity is most often the best way.

One way to keep it tidy is to declare your own object to window (window.myGlobals and put all the stuff you need into that one.

Comments

0

Placing your variables in global scope (as property of the window object in the case of browsers) is called namespace pollution and it is a bad form because it makes understanding the code much more difficult compared to nicely separated, modular code. Difficult to understand code leads to bugs and difficulties in maintenance.

There are reasons why you would place some variables in the global space (because they are needed by more than one script), but it's usually common practice to place as few as possible. For example the jQuery library places the jQuery object in the global scope. Anything else is self contained or exposed as properties of that object. jQuery plugins make use of that object and also offer an interface through it.

In your particular case, you had a bunch of information extracted from localStorage and placed into numbered variables. The solution with window['id' + i] is offered as a patch that alters little of your code, but it would be best if you encapsulated all the data in an array which you can access simply as data[i]. Note that this array is also in the global scope, but it is the only one in the global scope.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.