Which is better?

S

Simulacrom1

Is it better to use:

var hasDocumentLayers document.layers ? true : false;

and then use:

if( hasDocumentLayers ){....do something....}

Or it is better to always use

if( document.layers ){....do something....}

Also, which is faster? I'm thinking not having to index the document
class each time is faster - but if Javascript is pre-compiled into
something like pcode (or like Perl/PHP do it), then it might not matter
at all which way you did it because everything would have already been
looked up. If Javascript doesn't pre-compile and it interprets each
line as it gets to it, then I'm thinking the hasDocumentLayers would be
faster as it is only interpreted once and then just referenced
throughout the program.

Which? :)
 
R

RobG

Is it better to use:

var hasDocumentLayers document.layers ? true : false;

and then use:

if( hasDocumentLayers ){....do something....}

Or it is better to always use

if( document.layers ){....do something....}

It depends on your criteria for "better". Likely the "better" one is
the one that most closely follows your general code patterns and
principles.

Also, which is faster? I'm thinking not having to index the document
class each time is faster - but if Javascript is pre-compiled into
something like pcode (or like Perl/PHP do it), then it might not matter
at all which way you did it because everything would have already been
looked up.

Test it.

If Javascript doesn't pre-compile and it interprets each
line as it gets to it, then I'm thinking the hasDocumentLayers would be
faster as it is only interpreted once and then just referenced
throughout the program.

Browsers may compile javascript at run-time, each script element is
only loaded once, optimisations can occur then.

Test it. Given the following script:

function doStuff(n) {
var i = n;
var rpt =[];
var s = new Date();
while (i--) {
document.layers;
}
rpt.push('document.layers: ' + (new Date() - s));
i = n;
s = new Date();
var hasLayers = (document.layers)? true : false;
while (i--) {
hasLayers;
}
rpt.push('hasLayers: ' + (new Date() - s));
alert(rpt.join('\n'));
}

doStuff(1e5);


IE shows:
document.layers: 730
hasLayers: 16

Firefox 3:
document.layers: 1100
hasLayers: 2
 
T

Thomas 'PointedEars' Lahn

RobG said:
Simulacrom1 said:
Is it better to use:

var hasDocumentLayers document.layers ? true : false;

and then use:

if( hasDocumentLayers ){....do something....}

Or it is better to always use

if( document.layers ){....do something....}
[...]

Test it. Given the following script:

function doStuff(n) {
var i = n;
var rpt =[];
var s = new Date();
while (i--) {
document.layers;
}
rpt.push('document.layers: ' + (new Date() - s));
i = n;
s = new Date();
var hasLayers = (document.layers)? true : false;
while (i--) {
hasLayers;
}
rpt.push('hasLayers: ' + (new Date() - s));
alert(rpt.join('\n'));
}

doStuff(1e5);


IE shows:
document.layers: 730
hasLayers: 16

Firefox 3:
document.layers: 1100
hasLayers: 2

As expected.

1. `hasLayers', being declared a local variable, is a property of the
local Variable Object, and the effective scope chain for resolving
the identifier is shorter than the scope chain for `document.layers':

Scope Chain
---------------------------------------------------------------------
hasLayers --> local Variable Object

document.layers --> local Variable Object (no match)
--> Zero or more host objects in the scope chain
(possible match; in particular, there could be a
Window object having a `document' property)
--> Global Variable Object === Global Object
(possible match, in particular, the Global
Object may have a host-defined `document'
property)

2. `hasLayers' requires one property lookup less than `document.layers':

Property Lookups
-------------------------------------------------------------------
hasLayers [local Variable Object].hasLayers

document.layers [Host object or Global (Variable) Object].document
[Host object or Global (Variable) Object]
.document.layers

See the ECMAScript Language Specification, Edition 3 Final, section 10.1.4,
Scope Chain and Identifier Resolution, and corresponding DOM documentation.


PointedEars
 
M

Michael J. Ryan

Is it better to use:
var hasDocumentLayers document.layers ? true : false;
and then use:
if( hasDocumentLayers ){....do something....}

Or it is better to always use
if( document.layers ){....do something....}

Also, which is faster? I'm thinking not having to index the document
class each time is faster - but if Javascript is pre-compiled into
something like pcode (or like Perl/PHP do it), then it might not matter
at all which way you did it because everything would have already been
looked up. If Javascript doesn't pre-compile and it interprets each
line as it gets to it, then I'm thinking the hasDocumentLayers would be
faster as it is only interpreted once and then just referenced
throughout the program.

Which? :)

Creating a variable may be faster, but may obscure what you are really testing
for. Also, document.layers is an ancient NN-ism. IMHO, If
document.getElementById isn't supported, I don't support it. Spent way too
many years in IE4/NN4 hell to go back to it.
 
T

Thomas 'PointedEars' Lahn

RobG said:
Is it better to use:

var hasDocumentLayers document.layers ? true : false;
[...]
[...]
var hasLayers = (document.layers)? true : false;
while (i--) {
hasLayers;
}

BTW, there is little point in `? true : false' if no strict test is
performed on the assigned variable value later.


PointedEars
 
S

Simulacrom1

Michael said:
Creating a variable may be faster, but may obscure what you are really
testing for. Also, document.layers is an ancient NN-ism. IMHO, If
document.getElementById isn't supported, I don't support it. Spent way
too many years in IE4/NN4 hell to go back to it.

Well, first - it was just an example of other items like the
document.getElementById and so on. But secondly - I do (sadly) know of
several people who still use NN4 and IE4 & 5. Their machines are really
old but then these people aren't kids either. Kids probably wouldn't
touch anything other than the most current versions anyway. But there
are a lot of small companies who do still have very old machines either
because they can't afford the upgrade or they just don't want to do so.
And I just happen to work with both groups on a part-time basis. :)
(I do encourage them to upgrade - but only one or two of them have done
so. Other small companies [especially those being started by younger
people] do use current technologies. Older ones though tend to stick
with older technology. :-/ )

Thanks for the response though! :) I appreciate it! :)
 
S

Simulacrom1

Thomas said:
RobG said:
Simulacrom1 said:
Is it better to use:

var hasDocumentLayers document.layers ? true : false;

and then use:

if( hasDocumentLayers ){....do something....}

Or it is better to always use

if( document.layers ){....do something....}
[...]

Test it. Given the following script:

function doStuff(n) {
var i = n;
var rpt =[];
var s = new Date();
while (i--) {
document.layers;
}
rpt.push('document.layers: ' + (new Date() - s));
i = n;
s = new Date();
var hasLayers = (document.layers)? true : false;
while (i--) {
hasLayers;
}
rpt.push('hasLayers: ' + (new Date() - s));
alert(rpt.join('\n'));
}

doStuff(1e5);


IE shows:
document.layers: 730
hasLayers: 16

Firefox 3:
document.layers: 1100
hasLayers: 2

As expected.

1. `hasLayers', being declared a local variable, is a property of the
local Variable Object, and the effective scope chain for resolving
the identifier is shorter than the scope chain for `document.layers':

Scope Chain
---------------------------------------------------------------------
hasLayers --> local Variable Object

document.layers --> local Variable Object (no match)
--> Zero or more host objects in the scope chain
(possible match; in particular, there could be a
Window object having a `document' property)
--> Global Variable Object === Global Object
(possible match, in particular, the Global
Object may have a host-defined `document'
property)

2. `hasLayers' requires one property lookup less than `document.layers':

Property Lookups
-------------------------------------------------------------------
hasLayers [local Variable Object].hasLayers

document.layers [Host object or Global (Variable) Object].document
[Host object or Global (Variable) Object]
.document.layers

See the ECMAScript Language Specification, Edition 3 Final, section 10.1.4,
Scope Chain and Identifier Resolution, and corresponding DOM documentation.


PointedEars

Thanks! :)
 
S

Simulacrom1

RobG said:
Is it better to use:

var hasDocumentLayers document.layers ? true : false;

and then use:

if( hasDocumentLayers ){....do something....}

Or it is better to always use

if( document.layers ){....do something....}

It depends on your criteria for "better". Likely the "better" one is
the one that most closely follows your general code patterns and
principles.

Also, which is faster? I'm thinking not having to index the document
class each time is faster - but if Javascript is pre-compiled into
something like pcode (or like Perl/PHP do it), then it might not matter
at all which way you did it because everything would have already been
looked up.

Test it.

If Javascript doesn't pre-compile and it interprets each
line as it gets to it, then I'm thinking the hasDocumentLayers would be
faster as it is only interpreted once and then just referenced
throughout the program.

Browsers may compile javascript at run-time, each script element is
only loaded once, optimisations can occur then.

Test it. Given the following script:

function doStuff(n) {
var i = n;
var rpt =[];
var s = new Date();
while (i--) {
document.layers;
}
rpt.push('document.layers: ' + (new Date() - s));
i = n;
s = new Date();
var hasLayers = (document.layers)? true : false;
while (i--) {
hasLayers;
}
rpt.push('hasLayers: ' + (new Date() - s));
alert(rpt.join('\n'));
}

doStuff(1e5);


IE shows:
document.layers: 730
hasLayers: 16

Firefox 3:
document.layers: 1100
hasLayers: 2

After I had posted I thought to my self "Ya know - all ya gotta do is
write a little routine to do a timing test" and I went, "Yeah, that was
probably not a good post." :)

But I do want to say thank you very much for the test program. That is
quite a difference between the two. So (as Thomas pointed out)
Javascript must not pre-compile anything but interprets each line and
the look-up does take quite a bit longer. Ok - thanks! :)
 
T

Thomas 'PointedEars' Lahn

Simulacrom1 said:
[...] So (as Thomas pointed out)
Javascript must not pre-compile anything but interprets each line

A common misconception. The resolution of identifiers at runtime as
specified in the ECMAScript Specification does not preclude compilation.
In fact, JavaScript and JScript use a JIT-compiler to create bytecode
that is then interpreted by a virtual machine.
and the look-up does take quite a bit longer. [...]

The reason for that is a different one that you apparently think, though.


PointedEars
 
M

Michael J. Ryan

Well, first - it was just an example of other items like the
document.getElementById and so on. But secondly - I do (sadly) know of
several people who still use NN4 and IE4 & 5. Their machines are really
old but then these people aren't kids either. Kids probably wouldn't
touch anything other than the most current versions anyway. But there
are a lot of small companies who do still have very old machines either
because they can't afford the upgrade or they just don't want to do so.
And I just happen to work with both groups on a part-time basis. :)
(I do encourage them to upgrade - but only one or two of them have done
so. Other small companies [especially those being started by younger
people] do use current technologies. Older ones though tend to stick
with older technology. :-/ )

Thanks for the response though! :) I appreciate it! :)

A newer version of opera, or another micro-browser may do well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top