Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript optimization...global variables

I am making a webapp. I have a fairly basic question about javascript performance. Sometimes I need a global variable to store information that is used the entire time the website is open.

An example is a variable called needs_saved. It is true or false to say whether the page needs saved. I might have another variable called is_ie, ie_version, or space_remaining.

These are all variable that I need in various functions throughout the app.

Now, I know global variables are bad because they require the browser to search each level of function scope. But, I don't know if there is any better way to store values that are needed throughout the program.

I know I could create a global object called 'program_status' and give it the properties is_ie, ie_version, etc... But is this any better since it would first have to find my program_status object (stored as a global variable), and then the internal property?

Maybe I'm overthinking this.

Thanks

like image 630
Johnny Nogter Avatar asked Sep 28 '26 22:09

Johnny Nogter


1 Answers

You have nothing to worry about.

The performance impact of a global variable is minute.
Global variables are discouraged because they can make code harder to maintain.
In your case, they won't.

like image 98
SLaks Avatar answered Sep 30 '26 11:09

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!