Daniel Rucareanu wrote:
Why is it when I use Test.F.FF.FFF.FFFF() I get around 100ms and when I
get alias() I get around 280ms?
What is going on here? This is something I would have never anticipated
and I think that neither of you as well. Aliases whould work faster
then doing lockups at each iteration, right?
Not at all. That may be true for some C-like language with pointers and
an execution cache (a la processor cache). I do not know for sure of a
such language but a technical possibility is here. For a Java-like
language without pointers, byRef aliases only slow down the execution
as the engine gets one more reference to resolve (alias ref ref >
ref instead of ref ref ref). The difference is fractional, so feel
free to use aliases if it improves the code readability: but do not
expect any serious performance changes.
That can be a performance improvement using a byVal intermediary
variable rather than recalculations, the most known sample is
var len = something.length;
for (var i=0; i<len; ++i) {
//...
}
It is also beneficial to use byRef intermediary variable for DOM
references rather than using getElementById each time. The latter is
especially true for IE because their getElementById implementation is
an artificial build-up atop of IE's native schema (with ID'ed elements
automatically referenced in the Global scope) and about four times
slower than a ready to use reference (MSDN says sorry for it, but they
are really not and it doesn't help too much in neither case :-)
For within JavaScript references only (like in your case) that is the
matter of such fractions that pigs will become flying aces before you
get some practical impact out of it :-)
What practical is that IE'e schema is much quicklier in resolving
references, because they do not use ECMAScript standard for Function
objects. Instead they treat each and every function as expression. The
result of this expression is function reference added to the Global
scope, no matter how deep this function is burried inside and by how
many syntax signs is it surrounded.
var foo = function bar(){};
window.alert(typeof bar);
- as just a starting hint of what am I talking about, not as an
explanation.
This speeds up the lookup process, because the engine jumps from one
ref to another in the same Global scope. It also mean that IE schema is
rather indifferent to how many levels down to go, as it really stays on
the same top level.
It also means that for really speed crutial processes like say visible
points calculation for 3D SVG/VML graphics it is highly benefitial to
skip on "correctness of approach" and use only top level functions
calling each other. IE is indifferent, but FF will "appreciate it".
P.S.
Also if I uncomment this line: var alias = function(){},
from 280ms I get a drop to 265 ms?
The draw back of traditional timerON many loops timerOFF tests is
that any good engine is much more than just "execute a chunk - move to
next chunk". A good engine (and both FF and IE ones are) has a
look-ahead and code optimisation mechanics inside. So starting with the
second loop you are dealing not only with the test subject but also
with optimization mechanics differences.
And this is just an icing on the cake where the cake is Date object
working with the precision no better than one system tick (10ms - 60ms
depending on OS).