What is the Best Third-Party Camera App for Android?

When it comes to Camera apps, there are dozens of great options to choose … more

Google to Acquire XDA, Dev Rewards & Policy Changes

We are delighted to announce that starting on April 20th, a finalized deal with … more

Hi Locker to Help You With Lockscreen Headache

The lockscreen is a part of the OS that we see hundreds of times every day, whether … more

Must Have App Review: Spider Squisher Pro Extreme

Here on XDA TV we have a series we like to call Must Have Apps. These are apps that we … more
Post Reply Subscribe to Thread Email Thread

Global variable make app or game slow ?

26th July 2014, 07: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, 08: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, 08: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, 04: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, 03:07 PM |#5  
gegerhr's Avatar
Junior Member
Flag Zagreb
Thanks Meter: 0
 
10 posts
Join Date:Joined: Jul 2014
Donate to Me
More
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, 04: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, 04: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, 11:24 AM |#8  
Archer's Avatar
Retired Senior Moderator / Recognized Developer - (Un)Recognized Pole Dancer
Flag Northampton
Thanks Meter: 4,147
 
10
13,941 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.
26th December 2014, 10:00 PM |#9  
Junior Member
Thanks Meter: 1
 
14 posts
Join Date:Joined: Dec 2014
Wink
Global variables could be painful about 10-15 years ago. Nowadays computing machines so fast and advanced, even if it makes slower, you won't feel it. Dont worry about your scripts, if you have got good algorythms. Visual effect and animations slow your app, think about it.

Also i advice to use Singleton class for storing globals
12th February 2015, 10:17 AM |#10  
djscorchio's Avatar
Junior Member
Flag Vác
Thanks Meter: 3
 
11 posts
Join Date:Joined: Dec 2010
More
It's not really possible to answer this, not knowing exactly what are used as globals, but my first bet would be to say no, they won't slow down the app considerably unless you're storing "weird things" as globals. (For example, I don't think that having 10 booleans as globals would have measurable impact, it's another topic whether that would make any sense at all. Anyway, from what I know usually applications need to keep their state somehow and it's your job to figure out how to do that, using globals, singletons or whatever.) It's not going to be a good practice to overuse globals though, so I would avoid that to a degree.

More importantly you will need to learn how to detect the potential performance issues in your application: sometimes a badly written UI can cause much worse problems for sure, not to mention carelessly written networking routines etc.

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

Advanced Search
Display Modes