D
Dmitry A. Soshnikov
You don't exactly whether or not `setInterval' is the property of the
`window' (see in different browsers, I've already showed this in topic
related to `alert' property, here's the same):alert([
'setInterval' in window,
'setInterval' in this,
Object.prototype.hasOwnProperty.call(this, 'setInterval'),
Object.prototype.hasOwnProperty.call(window, 'setInterval'),'setTimeout' in window,
'setTimeout' in this,
Object.prototype.hasOwnProperty.call(this, 'setTimeout'),
Object.prototype.hasOwnProperty.call(window, 'setTimeout')
]);The most interesting results for you will be in Chrome and Opera.
Even in FF 3.5 the results are different, depending whether I run the
above code in Firebug or in a html page. For instance:
In Firebug: true,false,false,true,true,false,false,true
In a HTML page (with FF): true,true,true,true,true,true,true,true
So, maybe the problem resides in the 'this' keyword used in your
example and in which environment / widget the code is run.
I thought that it was obvious that using `this' value was meant the
global object as we were talking about global object and `window' host
object.
I think the correct test case should be:
<script type="text/javascript">
var GLOBAL = this;
alert([
'setInterval' in window,
'setInterval' in GLOBAL,
Object.prototype.hasOwnProperty.call(GLOBAL, 'setInterval'),
Object.prototype.hasOwnProperty.call(window, 'setInterval'),
'setTimeout' in window,
'setTimeout' in GLOBAL,
Object.prototype.hasOwnProperty.call(GLOBAL, 'setTimeout'),
Object.prototype.hasOwnProperty.call(window, 'setTimeout')
]);
</script>
No matter, as I repeat - `this' in global context means global itself.
The thing is that in Opera `setInterval' is the direct property of the
global but not the property of the `window' host object (at least by
the implementation of `in' and `hasOwnProperty' stuff, which sure in
this case is implementation dependent but it's not the main goal).
Using 'window.alert' instead of 'alert' would result in a tad faster
code.
The one question I have - are you completely sure about that or it
just a guess? Moreover, you should take in mind both parts:
theoretical and practical.
For me from this point of view theoretical part is always more
interesting. For the bases we should take `PrimaryExpression :
Identifier' (10.1.4 Scope Chain and Identifier Resolution, ES-3) and
`MemberExpression . Identifier' (11.2.1 Property Accessors, ES-3).
Comparing that two algorithms we don't even need to see the practical
results to statement that `some.thing' should resolve slower than
plain `thing' (in our case regarding to global object and `window'
host object in browser host environment).
So, just by the theory I can statement that `alert' is faster than
`window.alert'.
But. As `window' is the host object, and thus implementation depended,
we can use a simple test to check the theory (I use grouping operator
for do not call GetValue when resolving identifiers, you should know
about this; also test is running from the global execution context):
<script type="text/javascript">
var
n = 500000,
GLOBAL = this,
t1, t2, t3, t4, t5, t6
t = +new Date;
for (var k = n; k--
(alert);
t1 = +new Date - t;
t = +new Date;
for (k = n; k--
(window.alert);
t2 = +new Date - t;
t = +new Date;
for (k = n; k--
(this.alert);
t3 = +new Date - t;
t = +new Date;
for (k = n; k--
(GLOBAL.alert);
t4 = +new Date - t;
t = +new Date;
for (k = n; k--
(GLOBAL.window.alert);
t5 = +new Date - t;
t = +new Date;
for (k = n; k--
((GLOBAL.window || GLOBAL).alert);
t6 = +new Date - t;
alert(
'alert: ' + t1 + '\n' +
'window.alert: ' + t2 + '\n' +
'this.alert: ' + t3 + '\n' +
'GLOBAL.alert: ' + t4 + '\n' +
'GLOBAL.window.alert: ' + t5 + '\n' +
'(GLOBAL.window || GLOBAL).alert: ' + t6
);
</script>
Results on my machine in FF 3.5.6 WinXP:
alert: 78
window.alert: 1827
this.alert: 1116
GLOBAL.alert: 1127
GLOBAL.window.alert: 2430
(GLOBAL.window || GLOBAL).alert: 2460
Opera 10.10 WinXP:
alert: 204
window.alert: 500
this.alert: 250
GLOBAL.alert: 265
GLOBAL.window.alert: 453
(GLOBAL.window || GLOBAL).alert: 469
IE 8 WinXP (first dies saying script eats much memory, after continue
the follwing results):
alert: 843
window.alert: 2250
this.alert: 2375
GLOBAL.alert: 2313
GLOBAL.window.alert: 16891
(GLOBAL.window || GLOBAL).alert: 17937
Safari 4.0.3 (531.9.1) is the only one which resolves `window.alert'
and othes much faster than plain `alert' (but still `this.alert' is
faster than `window.alert'):
alert: 352
window.alert: 5
this.alert: 4
GLOBAL.alert: 4
GLOBAL.window.alert: 55
(GLOBAL.window || GLOBAL).alert: 58
Chrome 3.0.195.38 WinXP:
alert: 231
window.alert: 552
this.alert: 266
GLOBAL.alert: 290
GLOBAL.window.alert: 585
(GLOBAL.window || GLOBAL).alert: 598
So you can see that in most parts plain `alert' is faster than
`window.alert` (except Safari which I guess has some special mapping
for resolving `window' name which resolves very fast).
The only thing I can't understand how some can continue to propagate
in statement form saying "*should* be window.alert" (do you hear? -
*should*!). That at least no so deep in understanding how ES works
even regardless implementation dependent host objects.
If someone has a habit to write `window.alert' - that's OK (I also had
a habit to write `window.setTimeout'), but what's completely wrong
(and even stupid after all explanations) - is to statement to the
newbies on their "alert(...)" that "should be window.alert(...)"; the
same with corrections when the topic is fully different and doesn't
touch `window' host object
/ds