I think it's indeed a good feature to discuss for 2.0.
I've been working on this too and had quite reasonable performance  
when using a hash function that avoids collision.
The simplest implementation consists in storing array of [key, value]  
tuples indexed by `key.toString()` in a vanilla object.

i.e. :
        hash.set(true, value1);
        hash.set("true", value2);
is stored as:
        { "true": [ [true, value1], ["true", value2] ] }

Custom classes should override their `toString` method to be  
efficiently stored in such a structure, otherwise fetching a value  
would be as slow as a `Array#find` call.
Having real hashes allows to implement sets too.


On 10 sept. 2009, at 22:46, T.J. Crowder wrote:

> Hi James,
> You're right, Hash does indeed rely on underlying JavaScript vanilla
> objects, which can only use strings as keys.
> Supporting using objects as keys would be a complete rewrite, and a
> fairly inefficient one in terms of runtime performance (I did it once,
> and abandoned it as not worthwhile).  I'm not aware of any plans to do
> so.  I suspect you'd be better off (from a performance standpoint)
> building toString / fromString support into your keys.
> --
> T.J. Crowder
> tj / crowder software / com
> www.crowdersoftware.com
> On Sep 10, 7:07 pm, James Aimonetti <james.aimone...@gmail.com> wrote:
>> Core,
>> Wanted to ask about why Hash doesn't support Objects as keys? I  
>> believe
>> it relies on Javascript using the toString on the object to set the
>> key's value, rather than a true hash.
>> var key1 = new Object();
>> var key2 = new Object();
>> var h = $H();
>> h.set(key1, "First");
>> h.set(key2, "Second");
>> h.get(key1) // -> Second
>> Is there any plan to support true object-as-key ability? I couldn't  
>> find
>> discussion on the topic searching the group.
>> --
>> James Aimonetti
>> mobile: 314.809.6307
>> work: 540.459.2220
>> email: james.aimone...@gmail.com
>> website:http://jamesaimonetti.com
> >

You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to