TinyBase logoTinyBase β

getHash

The getHash function returns a deterministic hash of a string, which can be used to quickly compare the content of two entities.

getHash(string: string): Hash
TypeDescription
stringstring

The string to hash.

returnsHash

A hash of the string.

This is used internally within TinyBase (for example in the mergeable-store module) to quickly compare the content of two objects, but is also useful for applications that need to represent strings in a deterministic (though not cryptographically safe) way.

The hash uses the Fowler–Noll–Vo hash approach, producing a JavaScript number. It is not guaranteed to be unique by any means, but small changes in input generally produce large changes in output, so it should be sufficient for most purposes.

Example

This example gets the hash of two similar strings.

import {getHash} from 'tinybase';
console.log(getHash('Hello, world!'));
// -> 3985698964
console.log(getHash('Hello, world?'));
// -> 3549480870

Since

v6.2.0