Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:51:24+00:00 2026-05-14T08:51:24+00:00

Does the DOM have a hash-table of elements whose keys are the elements’ ids?

  • 0

Does the DOM have a hash-table of elements whose keys are the elements’ ids?
I want to know if document.getElementById looks up a hash table or traverses the entire tree.
Is this behavior different across browsers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:51:25+00:00Added an answer on May 14, 2026 at 8:51 am

    Implementations are free to do whatever they like, but since id is defined as a unique value, it would seem sensible to use a hash map or similiar lookup mechanism rather than traversal. What seems sensible from the outside, though, may not be when you get into the plumbing of building a complex web browser with many (sometimes conflicting) imperatives.

    I did an easy but very simplistic test (see page at end of answer). It’s very simplistic not least because we don’t know that browsers don’t cache previous results.

    Chrome 4.1.249.1059 reports:

    ID: 0.0082ms per lookup
    Tag: 0.0249ms per lookup
    

    So, dramatically faster by ID than tag name.

    IE7 reports:

    ID: 2.4438ms per lookup
    Tag: 0.0437ms per lookup
    

    So dramatically faster by tag name than ID (but remember IE7 has a broken concept of getElementById; this is fixed in IE8).

    IE8 (on a different machine, don’t compare absolutes, we’re looking at diffs within the browser tested) reports:

    ID: 1.1335ms per lookup
    Tag: 0.0287ms per lookup
    

    So about the same as IE7.

    Firefox 3.6.3 reports:

    ID: 0.0042ms per lookup
    Tag: 0.006ms per lookup
    

    So it doesn’t care that much (on repeated requests; again, it may be caching).

    Opera 10.5.1 reports:

    ID: 0.006ms per lookup
    Tag: 1.467ms per lookup
    

    Dramatically faster by ID than tag name.

    Make of those results what you will. A more complex test case would be needed to really infer the mechanisms. Of course, in at least two of those cases (Firefox and Chrome), we can go look at the source. CMS kindly points us to the WebKit and Firefox implementations (and looking at it, my suspicion about caching may have been on the money).

    Test page:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Test Page</title>
    <style type='text/css'>
    body {
        font-family: sans-serif;
    }
    #log p {
        margin:     0;
        padding:    0;
    }
    </style>
    <script type='text/javascript'>
    window.onload = pageInit;
    function pageInit() {
        document.getElementById('btnGo').onclick = btnGoClick;
    }
    function btnGoClick() {
    
        log("Testing...");
        setTimeout(run, 0);
    }
    
    function run() {
        var count, time;
    
        try {
            // Warm up
            testid(10);
            testtag(10);
    
            // Test
            count = 10000
            time = testid(count);
            log("ID: " + (time / count) + "ms per lookup");
            time = testtag(count);
            log("Tag: " + (time / count) + "ms per lookup");
        }
        catch (e) {
            log("Error: " + (e.message ? e.message : String(e)));
        }
    }
    
    function testid(count) {
        var start;
    
        start = new Date().getTime();
        while (count-- > 0) {
            if (!document.getElementById('fred')) {
                throw "ID 'fred' not found";
            }
        }
        return new Date().getTime() - start;
    }
    
    function testtag(count) {
        var start;
    
        start = new Date().getTime();
    
        while (count-- > 0) {
            if (document.getElementsByTagName('em').length == 0) {
                throw "Tag 'em' not found";
            }
        }
        return new Date().getTime() - start;
    }
    
    function log(msg) {
        var log = document.getElementById('log');
        log.innerHTML += "<p>" + msg + "<\/p>";
    }
    </script>
    </head>
    <body><div>
    <input type='button' id='btnGo' value='Go'>
    <div id='log'></div>
    <hr>
    <div>test test<span>test<span>test<span>test<span>test</span></span></span></span></div>
    <div>test test<span>test<span>test<span>test<span>test</span></span></span></span></div>
    <div>test test<span>test<span>test<span>test<span>test</span></span></span></span></div>
    <div>test test<span>test<span>test<span>test<span>test</span></span></span></span></div>
    <!-- repeat the above a couple of thousand times; I had about 2,200 -->
    <div>test test<span>test<span>test<span>test<span>test</span></span></span></span></div>
    <div>test test<span>test<span>test<span>test<em id='fred'>test</em></span></span></span></div>
    </div></body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, a better solution would to be use a richer… May 15, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer You'll want to pick up a copy of Amit Singh's… May 15, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer Answer to original question: You need to apply it on… May 15, 2026 at 6:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.