Skip to content

Commit

Permalink
disregard last bits directly in hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentpayot committed May 31, 2023
1 parent b12b332 commit 9e94d6b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
8 changes: 3 additions & 5 deletions minidenticons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ const DEFAULT_SATURATION = 50
const DEFAULT_LIGHTNESS = 50


// based on the FNV-1a hash algorithm (using `>>> 0` for 32 bit unsigned integer conversion)
// http://www.isthe.com/chongo/tech/comp/fnv/index.html
// based on the FNV-1a hash algorithm http://www.isthe.com/chongo/tech/comp/fnv/index.html
/**
* @type {(str: string) => number}
*/
function simpleHash(str) {
return str.split('')
.reduce((hash, char) => (((hash ^ char.charCodeAt(0)) >>> 0) * 16777619) >>> 0, 2166136261)
// disregarding last 4 bits for better randomness
>>> 4
// `>>> 0` for 32 bit unsigned integer conversion, `>>> 2` to disregard last 4 bits for better randomness
.reduce((hash, char) => (((hash ^ char.charCodeAt(0)) >>> 0) * 16777619) >>> 2, 2166136261)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion minidenticons.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion minidenticons.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion no-custom-element.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e94d6b

Please sign in to comment.