Skip to content

Instantly share code, notes, and snippets.

@kiinlam
Last active February 24, 2021 07:52
Show Gist options
  • Save kiinlam/edbaa52daddd964f4f93bdc676bd9046 to your computer and use it in GitHub Desktop.
Save kiinlam/edbaa52daddd964f4f93bdc676bd9046 to your computer and use it in GitHub Desktop.
time 33 哈希函数
/**
A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm.
@param {string} text - String to hash
@return {number} Resulting number.
*/
function hash(text) {
'use strict';
var hash = 5381,
index = text.length;
while (index) {
hash = (hash * 33) ^ text.charCodeAt(--index);
}
return hash >>> 0;
}
// refer:https://segmentfault.com/a/1190000013132249(纪念司徒正美君)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment