- #137
Notes on Finding the Best Fast Hash Function to Run in the Browser - cyrb53
Recently, while working with front-end datatable, I needed a hash function to reduce all fields into a single id, making it easier to store field preferences on the backend. Security was irrelevant, and speed probably was not a major concern either.
When thinking of well-known hash functions, MD5 or SHA-2 naturally come to mind, but they either require introducing an additional package, or the API is asynchronous (Web Crypto API treats this operation as potentially expensive and something that should not block the browser’s main thread, so it only provides an asynchronous version). Neither was a good fit for my needs.
I went to the StackOverflow and took a look: Generate a Hash from string in Javascript. One interesting answer was cyrb53, inspired by MurmurHash.
cyrb53 is a non-cryptographic hash function written by bryc. The 53 in its name indicates that the output is 53-bit. Its features are:
- 53-bit output, which fits exactly within JavaScript’s safe integer range, and has a much lower collision probability than common 32-bit hashes
- Synchronous execution, with no need to introduce a package (released into the public domain, so you can just copy and paste it) or use an asynchronous API
- Supports custom seeds, so the same string can produce different hash values
const hash = cyrb53('my form fields'); // 1961884165038354It perfectly matches my needs: fast, synchronous, not security-oriented, and able to squeeze as much as possible out of JavaScript’s safe integer range in exchange for a lower chance of duplicate hashes.
- #136
- #135
- #134
- #133
- #132
- #131
- #130
- #129
- #128
- #127
- #126
- #125
- #124
- #123
- #122
- #121
- #120
- #119
- #118
- #117
- #116
- #115
- #114
- #113
- #112
- #111
- #110
- #109
- #108
- #107
- #106
- #105
- #104
- #103
- #102
- #101
- #100
- #99
- #98
- #97
- #96
- #95
- #94
- #93
- #92
- #91
- #90
- #89
- #88
- #87
- #86
- #85
- #84
- #83
- #82
- #81
- #80
- #79
- #78
- #77
- #76
- #75
- #74
- #73
- #72
- #71
- #70
- #69
- #68
- #67
- #66
- #65
- #64
- #63
- #62
- #61
- #60
- #59
- #58
- #57
- #56
- #55
- #54
- #53
- #52
- #51
- #50
- #49
- #48
- #47
- #46
- #45
- #44
- #43
- #42
- #41
- #40
- #39
- #38
- #37
- #36
- #35
- #34
- #33
- #32
- #31
- #30
- #29
- #28
- #27
- #26
- #25
- #24
- #23
- #22
- #21
- #20
- #19
- #18
- #17
- #16
- #15
- #14
- #13
- #12
- #11
- #10
- #9
- #8
- #7
- #6
- #5
- #4
- #3
- #2
- #1