getelementbyid vs index
In my intranet web app there is a table of items which currently uses the id tag to select the row intended, selection is at a rate of 5-10 selections/sec. The method currently used may cause some inconsistencies time to time due to the async nature and it is very hard to sync it correctly.
The question is selection by index (via eq tag in jquery) faster than selection by ID (via getElementByID)?
I did some test and it sho开发者_C百科wed that id is slightly faster but I thought I would ask the community for confirmation.
It would seem to me although against your conclusion that getElementByID would be faster since jquery is built on top of javascript although I may not know something about jquery needed to answer this question.
Boils down to raw javascript versus a layer on top of javascript and javascript by itself will always be king in this competition.
Nothing in jQuery is faster than getElementById
or any other built in DOM selector function. Every time you invoke $
you are incurring a performance penalty. Granted, if your jQuery selector is as simple as '#somediv', the difference will be slight. But if you are tuning for performance, and really want to eke out every last bit of performance, don't use jQuery.
If you absolutely need jQuery's wrapper functions, call $(document.getElementById('somediv'));
and assign the result to a variable that you can reuse.
Though without seeing your code it is hard to say how you could best optimize it or even if jQuery is the main bottleneck.
精彩评论