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

  • SEARCH
  • Home
  • 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 8471985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:56:40+00:00 2026-06-10T16:56:40+00:00

Some languages implement hash tables which can use anything, not just strings, as a

  • 0

Some languages implement hash tables which can use anything, not just strings, as a key. In JavaScript, you are restricted to strings and numbers. Is the lookup for this kind of implementation still O(1)? Is there an implementation in JavaScript?

  • 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-06-10T16:56:41+00:00Added an answer on June 10, 2026 at 4:56 pm

    There’s obviously a lot of misunderstanding around this subject. In JavaScript, they keys of objects are actually only strings (see §8.10). Anything (including numbers) used as an object key is converted to string. Thus, obj[1] and obj["1"] are equivalent.

    Internally, many JS implementations use some kind of hash table to enable quick lookup of object properties. V8 actually generates hidden C++ classes that allows lookups to execute in a single CPU instruction. Either way, it’s safe to assume that object property access is fast and near O(1).

    As a consequence of all object property keys being converted to string, you can only use things that convert to string as a key. Since numbers naturally convert to string, they work fine. Same for booleans. Dates work too, since Date instances convert to unique strings (although there are edge cases to be aware of).

    However, objects do not convert into meaningful strings. For example:

    var o = {};
    o[{a:1}]='some value';
    

    Actually results in:

    o = { '[object Object]': 'some value' }
    

    because of the string conversion rules. Since every object converts to [object Object], adding many objects as keys to another object will result in an object with only one property.

    Of course, it’s still possible to use an object as a key. You just have to override the default implementation of toString — in effect, creating your own hashing algorithm. For example,

    function ComplexKey(a,b) {
        this.a = a;
        this.b = b;
    }
    
    ComplexKey.prototype.toString = function() {
        return this.a + ':' + this.b;
    }
    
    var o = {};
    o[new ComplexKey(1,2)] = 'x';
    o[new ComplexKey(3,4)] = 'y';
    

    Results in:

    o = {
        '1:2': 'x',
        '3:4': 'y'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking into designing some Domain Specific Languages which I will probably implement
How can I implement an enumeration type (spelled enum in some languages) in Python?
I think that some newer languages like JS can do this natively, but I
Is it harder (or impossible) to implement mutation testing in some languages than others?
Some people say that every programming language has its complexity budget which it can
I use codenameone to develop my mobile application. In this application I implement some
Can someone please shed some light on how popular languages like Python, Ruby implements
In some languages, like php, you don't need to manually initialize each dimension of
In some languages where you cannot override the () operator, I have seen methods
I know some languages allow this. Is it possible in C++?

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.