On Mar 27, 9:35 am, Hector Virgen <djvir...@gmail.com> wrote:
[...]
> As for speed, I would imagine that event.element() is faster than
> $('object_id') because even.element() is just accessing a property, instead
> of searching the dom for a node. But I could be wrong on this :)
Logic would say that you are. $() essentially a wrapper for
getElementById, and since that function is fundamental to the DOM
Core, it is likely highly optimised in browsers. Rather than having
to "search the DOM" I expect the browser creates an indexed list of
all elements with an ID and that getElementById will perform an
indexed lookup. The $ function may be slower in browsers like IE
where all of Prototpye.js's element methods must be added to the
element.
event.element is not "just accessing a property". It is a function
call, and as a consequence of adding a bunch of methods to the event
object and also attempting to accommodate the differences in the event
models of a few browsers, it does more work that the $ function.
Therefore there is no reason to believe it will be faster than
getElementById.
In any case, it doesn't take much to test:
<script type="text/javascript">
window.onload = function() {
$('button0').observe('click', function(e){
var el, i, num = 10000;
var msg = [];
var start;
start = new Date(), i = num;
while (i--) {el = e.element()}
msg.push('e.element: ' + (new Date() - start));
start = new Date(), i = num;
while (i--) {el = $('button0');}
end = new Date();
msg.push('$(id): ' + (new Date() - start));
start = new Date(), i = num;
while (i--) {el = e.target || e.srcElement;}
end = new Date();
msg.push('e.target or srcElement: ' + (new Date() - start));
alert(msg.join('\n'));
});
}
</script>
<input type="button" id="button0" value="Run test 0">
The above indicates that $(id) is twice as fast as event.element() in
IE, and 6 times faster in Firefox. If speed *really* matters, and you
know what you are doing, access the property directly (the third test
above).
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---