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
Type | Description | |
---|---|---|
string | string | The string to hash. |
returns | Hash | 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