[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Performance question

Joey@still_Learning.invalid

8/29/2015 11:12:00 PM

I'm attempting to optimize a lengthy ECMA script for speed. Please
pardon my ignorance...

I have multiple instances of document.getElementById('id').checked or
value.

Q1: When running the script, does it search through a table for the
address, or does it only do it once during compilation?

Q2. Moot if Q1 answer is 'only once.' If I declare a global variable
as 'var xxx = document.getElementById('id'),' and use it throughout
the script as 'xxx.value or xxx.checked,' does this improve
performance?

Thanks in advance for helping and not beating me up.
--
Joey
5 Answers

ram

8/29/2015 11:25:00 PM

0

"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
> If I declare a global variable
>as 'var xxx = document.getElementById('id'),' and use it throughout
>the script as 'xxx.value or xxx.checked,' does this improve
>performance?

One does indeed expect such an improvement, but the
best thing is to measure it under different browsers.

Joey@still_Learning.invalid

8/30/2015 4:50:00 PM

0

On 29 Aug 2015 23:24:49 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:

>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>> If I declare a global variable
>>as 'var xxx = document.getElementById('id'),' and use it throughout
>>the script as 'xxx.value or xxx.checked,' does this improve
>>performance?
>
> One does indeed expect such an improvement, but the
> best thing is to measure it under different browsers.

Thank you.
--
Joey

Thomas 'PointedEars' Lahn

8/30/2015 8:16:00 PM

0

Stefan Ram wrote:

> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>> If I declare a global variable
>>as 'var xxx = document.getElementById('id'),' and use it throughout
>>the script as 'xxx.value or xxx.checked,' does this improve
>>performance?
>
> One does indeed expect such an improvement, but the
> best thing is to measure it under different browsers.

There is no good reason to believe that omitting a function call in favor of
a variable access would not improve performance. It is unnecessary to
measure this.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

8/30/2015 8:20:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Stefan Ram wrote:
>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>> If I declare a global variable
>>>as 'var xxx = document.getElementById('id'),' and use it throughout
>>>the script as 'xxx.value or xxx.checked,' does this improve
>>>performance?
>>
>> One does indeed expect such an improvement, but the
>> best thing is to measure it under different browsers.
>
> There is no good reason to believe that omitting a function call in favor
> of
> a variable access would not improve performance. It is unnecessary to
> measure this.

Also, the variable may be declared, but the reference to the object should
be passed to functions and methods by parameter, which should be accessed
instead. This shortens the effective scope chain and improves performance
again.

The module pattern even removes the need for the variable to be declared
global.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

8/30/2015 8:22:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Stefan Ram wrote:
>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>> If I declare a global variable
>>>as 'var xxx = document.getElementById('id'),' and use it throughout
>>>the script as 'xxx.value or xxx.checked,' does this improve
>>>performance?
>>
>> One does indeed expect such an improvement, but the
>> best thing is to measure it under different browsers.
>
> There is no good reason to believe that omitting a function call in favor
> of a variable access would not improve performance. It is unnecessary to
> measure this.

Also, the variable may be declared, but the reference to the object should
be passed to functions and methods by parameter, which should be accessed
instead. This shortens the effective scope chain and improves performance
again.

The module pattern even removes the need for the variable to be declared
global.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.