0

While hashing is pretty fast already (looking at What integer hash function are good that accepts an integer hash key?), wondering if programs ever cache the hash key so you can just directly go to the array index instead.

var hashKey = hash('foo')
hashArray[hashKey]

If not, wondering why.

Lance
  • 75,200
  • 93
  • 289
  • 503

1 Answers1

1

In general, if an object contains an integer that will be used for hash-table lookups, the extra costs associated with storing a pre-computed hash of the integer in the object tend to exceed the costs of simply computing it when needed. For objects that will only be used for holding integers, it's usually not worth pre-computing the hash.

On the other hand, if a generic object may be used either for holding integers or other things that may need to be looked up in a hash table, it may be beneficial to store the hash of the object in question. If the generic object is being used to hold something whose hash is cheap to compute, the cost of storing the hash might be essentially wasted, but if the object might be used to store either integers or very long strings, the benefits of storing the hash might outweigh the costs.

supercat
  • 77,689
  • 9
  • 166
  • 211