Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Global variable make app or game slow ?

OP indietab

26th July 2014, 08:55 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: Jul 2014
HI
there as you can see the title what i am looking for

Global variable make app or game slow ?
Thanks
15th August 2014, 09:13 PM   |  #2  
Hvedrung's Avatar
Junior Member
Flag Brussels
Thanks Meter: 2
 
8 posts
Join Date:Joined: Jul 2013
No.
I don't know why it should. Using many global variable is not a good practice, you should avoid it but it shouldn't slow your app/game.
23rd August 2014, 09:23 PM   |  #3  
Angelkom's Avatar
Member
Flag Struga
Thanks Meter: 34
 
99 posts
Join Date:Joined: Jan 2013
More
Quote:
Originally Posted by indietab

HI
there as you can see the title what i am looking for

Global variable make app or game slow ?
Thanks

Yes they slow it because the global variables cannot be collected with the garbage collector, so the app or game becomes heavy.
29th August 2014, 05:47 PM   |  #4  
Junior Member
Thanks Meter: 1
 
12 posts
Join Date:Joined: Aug 2014
The primary reason why global variables are discouraged in javascript is because, in javascript all code share a single global namespace, also javascript has implied global variables ie. variables which are not explicitly declared in local scope are automatically added to global namespace. Relying too much on global variables can result in collisions between various scripts on the same page. It is better to avoid them.
2nd September 2014, 04:07 PM   |  #5  
Junior Member
Thanks Meter: 0
 
8 posts
Join Date:Joined: Jul 2014
Smile
Quote:
Originally Posted by indietab

HI
there as you can see the title what i am looking for

Global variable make app or game slow ?
Thanks

You should run test to see usage of your memory, to many globals with lots of data can slow you down. You should use it for constatnts.
Keep the memory optimal, and make sure you dont have any memory leaks.

Important, you need way to see memory usage.
17th September 2014, 05:50 PM   |  #6  
Junior Member
Thanks Meter: 6
 
26 posts
Join Date:Joined: Sep 2014
In short: no

However, you should avoid having large variables arrays, lists, etc. that you do not use. As the global variables will always have a reference to them, the garbage collector doesn't destroy them (as mentioned before), so they keep occupying memory unless you clear them yourself (for example by setting them to null).

If you need the functionality (and the values contained in the variables), it doesn't really matter where you define them, just know that you have to delete them (variableRef = null) if you dont need them anymore...

Have fun.
23rd September 2014, 05:42 AM   |  #7  
Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: Sep 2014
Quote:
Originally Posted by indietab

HI
there as you can see the title what i am looking for

Global variable make app or game slow ?
Thanks

I doubt it would
30th September 2014, 12:24 PM   |  #8  
Archer's Avatar
Senior Moderator / Recognized Developer - (Un)Recognized Pole Dancer
Flag Northampton
Thanks Meter: 4,117
 
10
13,913 posts
Join Date:Joined: Jul 2008
More
It's actually not possible to answer your question appropriately without more information. For example, if you have
Code:
window.x = 1;
at the beginning of your script then it won't slow anything down. If you have this though
Code:
window.x = [];
for (var i = 0; i < 999999999; i++) {
    window.x.push("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
then that will obviously have an impact.

Also, do your variables *need* to be global, or do you not know a better way to do something, or are you just being lazy?

Context and a description would help, otherwise there's no correct answer.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes