4

It is said that DOM traversal is high cost, so you should avoid it wherever possible. I think, however, retrieving DOM element by id must be very low cost, because most browser might have a table which associate elements with id internally. So almost always, we can ignore performance cost of this operation. Is this wrong?

Thanks.

Takahiro Hozumi
  • 701
  • 5
  • 11

1 Answers1

4

Chrome and Firefox appear to create a map for IDs, but at least as of ie8 it seems that Explorer still suffers from performance issues. Mike Blandford ran some benchmarking that at least implies that IE isn't using an array map, so its performance suffers significantlly:

JavaScript: document.getElementById slow performance?

To quote:

Here are the results (for a page with 10,000 elements on it):

IE8 getElementById: 0.4844 ms IE8 id array lookup: 0.0062 ms

Chrome getElementById: 0.0039 ms Chrome id array lookup: 0.0006 ms

he goes on to say the FF is much the same as chrome. Granted, .5ms is hardly much to worry about in most scenarios, but for large trees and frequent calls it can become an issue (some have pointed out that you can us JS to create a caching system for elements based on ID which will significantly improve performance in some browsers).

Community
  • 1
  • 1
Ben D
  • 14,321
  • 3
  • 45
  • 59