AlgoViz

Hash usage

easy 16 min

Hash maps and hash sets find a key by computing where it lives instead of searching for it, so insert, lookup and delete cost O(1) on average.

Where it shows up

The same pattern appears across everyday software.

  • Database indexes

    A hash index maps a column value to the row's location, so an exact-match query such as WHERE id = 340 is one lookup instead of a full table scan. B-tree indexes are used instead when queries need ranges or sorting.

  • Web and CDN caching

    Browsers, CDNs and caches such as Redis or Memcached store responses keyed by URL or request. A repeat request is a hash lookup instead of a trip to the origin server.

  • Search engines

    An inverted index maps every word to the set of documents that contain it. A query looks up each word and intersects the sets, without scanning a single document.

  • Password storage

    Sites store a slow cryptographic hash of each password (bcrypt, scrypt, Argon2) keyed by user, then compare hashes at login. This is a different kind of hash from a dict's: designed to be slow and one-way, not fast and spread out.

  • App state

    Front-end stores such as Redux keep entities normalised in objects keyed by id, so updating or reading one item is a key lookup rather than a search through an array.

  • Game development

    Spatial hashing keys objects by their grid cell, a (row, col) tuple, so collision checks only compare objects in the same or neighbouring cells. Asset and entity lookups by name or id are dicts too.