chad3814 / node-hashtable

Native hashtable interface for when V8 objects can't take the heat
MIT License
106 stars 41 forks source link

treating '001' and '1' as same key #26

Closed amitguptagwl closed 8 years ago

amitguptagwl commented 8 years ago

I tried to put data with key '001' and then '1'. When I try to get using same keys it always return same data which is inserted first.

chad3814 commented 8 years ago

as strings or as numbers? because as numbers they are the same.

> table.put('001', 'double oh one')
HashTable {}
> table.keys()
[ '001' ]
> table.put('1', 'just one')
HashTable {}
> table.keys()
[ '1', '001' ]
> table.get('1')
'just one'
> table.get('001')
'double oh one'
> table.get(1)
'just one'
> table.get(001)
'just one'
> 

when you use primitive values (string, boolean, number) for the key, it uses the string representation; non-primitive values (everything else, descendent of Object) for the key, it uses the identity hash (built in to node, accessible from native code, a unique integer).

If you need to differentiate between '1' and 1, I suggest you use the package es6-native-map; it is based off of this package and adheres more to the ES6 Map standard. Here's an example:

> Map = require('./')
[Function: NodeMap]
> m = new Map
NodeMap { size: 0 }
> m.set(1, 'integer one')
NodeMap { 1 => 'integer one', size: 1 }
> m.set('1', 'string one')
NodeMap { '1' => 'string one', 1 => 'integer one', size: 2 }
> m.get(1)
'integer one'
> m.get('1')
'string one'
> 
chad3814 commented 8 years ago

es6-native-map is on npm at https://www.npmjs.com/package/es6-native-map and on github at https://github.com/chad3814/es6-native-map

amitguptagwl commented 8 years ago

Thanks. For your quick response. I apologize to raise the issue before debugging myself. I actually was calling the hashtable incorrectly. Hence the issue occurred. Many thanks for your quick response.