Using app. within an Application class

Forums for specific tips, techniques and example code
Stokes
Posts: 66
Joined: Wed Oct 13, 2010 2:06 pm
Location: QLD, Australia

Using app. within an Application class

Postby Stokes » Tue May 10, 2011 4:54 pm

I have found that if you use:

app.

Within an application class it slows it down unnecessarily. You don't need to have app. within an application class so if you have code using this delete it out for a speed improvement. I found a 30% gain by getting rid of it.

Hope this is useful for someone.

JohnP
Posts: 73
Joined: Mon Sep 28, 2009 8:41 am
Location: Christchurch

Re: Using app. within an Application class

Postby JohnP » Wed May 11, 2011 9:49 am

Yes, this can save some time. Of course the percentage saved depends on what you are doing. The same goes for any class - it pays to avoid a lot of unnecessary dereferencing.

A few years ago, my team found a 3% savings in the application overall by changing this.

One downside is when you copy lines of code around - you may have to add or subtract "app." when you copy a line.

Callum
Posts: 5
Joined: Fri May 27, 2011 9:56 am
Location: Auckland

Re: Using app. within an Application class

Postby Callum » Fri May 27, 2011 10:04 am

The same goes for any class - it pays to avoid a lot of unnecessary dereferencing.
Does calling self.xxx cause unnecessary dereferencing?

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Using app. within an Application class

Postby BeeJay » Fri May 27, 2011 10:20 am

It depends on how many times you're referencing self.xxx in your method and whether or not self.xxx will ever change during the execution of that method.

For example, if self.xxx never changes during your method execution and you're dereferencing it multiple times, then it can be considerably more efficient to copy self.xxx into a local variable and use the local variable elsewhere in the method instead of self.xxx. This is especially the case if the dereferencing of self.xxx is inside a "large" while or foreach loop.

Cheers,
BeeJay.

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Using app. within an Application class

Postby allistar » Tue Jun 14, 2011 9:02 am

I would have liked to think that the JADE compiler or interpreter would have this efficiency built in.

JohnP
Posts: 73
Joined: Mon Sep 28, 2009 8:41 am
Location: Christchurch

Re: Using app. within an Application class

Postby JohnP » Wed Jun 15, 2011 4:12 pm

Using "self." does not cause unnecessary dereferencing, if that's what you were asking, Callum. For example, these two lines will have equivalent speed:

if myName = "Smith" then
if self.myName = "Smith" then

However, using "app." within an Application subclass does cause extra overhead. The last of these three lines will be more expensive than the first two (the method is on an Application subclass):

if myName = "Smith" then
if self.myName = "Smith" then
if app.myName = "Smith" then


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 4 guests