Skip to content

Commit cd8d87f

Browse files
committed
Try djb2 hash
1 parent 0fcd413 commit cd8d87f

File tree

2 files changed

+91
-69
lines changed

2 files changed

+91
-69
lines changed

src/basic/hash-murmur.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copied from https://github.com/emotion-js/emotion/blob/master/packages/hash/src/index.js
2+
3+
const murmur2 = (str) => {
4+
// 'm' and 'r' are mixing constants generated offline.
5+
// They're not really 'magic', they just happen to work well.
6+
7+
// const m = 0x5bd1e995;
8+
// const r = 24;
9+
10+
// Initialize the hash
11+
12+
var h = 0;
13+
14+
// Mix 4 bytes at a time into the hash
15+
16+
var k,
17+
i = 0,
18+
len = str.length;
19+
for (; len >= 4; ++i, len -= 4) {
20+
k =
21+
(str.charCodeAt(i) & 0xff) |
22+
((str.charCodeAt(++i) & 0xff) << 8) |
23+
((str.charCodeAt(++i) & 0xff) << 16) |
24+
((str.charCodeAt(++i) & 0xff) << 24);
25+
26+
k =
27+
/* Math.imul(k, m): */
28+
(k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16);
29+
k ^= /* k >>> r: */ k >>> 24;
30+
31+
h =
32+
/* Math.imul(k, m): */
33+
((k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16)) ^
34+
/* Math.imul(h, m): */
35+
((h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16));
36+
}
37+
38+
// Handle the last few bytes of the input array
39+
40+
switch (len) {
41+
case 3:
42+
h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
43+
case 2:
44+
h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
45+
case 1:
46+
h ^= str.charCodeAt(i) & 0xff;
47+
h =
48+
/* Math.imul(h, m): */
49+
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16);
50+
}
51+
52+
// Do a few final mixes of the hash to ensure the last few
53+
// bytes are well-incorporated.
54+
55+
h ^= h >>> 13;
56+
h =
57+
/* Math.imul(h, m): */
58+
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16);
59+
60+
return ((h ^ (h >>> 15)) >>> 0).toString(36);
61+
}
62+
63+
// Copied from https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/utils/hash.js
64+
65+
const SEED = 5381;
66+
67+
// When we have separate strings it's useful to run a progressive
68+
// version of djb2 where we pretend that we're still looping over
69+
// the same string
70+
const phash = (h, x) => {
71+
let i = x.length;
72+
73+
while (i) {
74+
h = (h * 33) ^ x.charCodeAt(--i);
75+
}
76+
77+
return h;
78+
};
79+
80+
// This is a djb2 hashing function
81+
const hash = (x) => {
82+
return phash(SEED, x);
83+
};
84+
85+
// switch between readable vs faster & smaller classnames depending on environment
86+
const readableHash = (value) => CSS.escape(value).replace(/\\./g, '_')
87+
88+
export default murmur2

src/basic/hash.js

+3-69
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,4 @@
1-
// Copied from https://github.com/emotion-js/emotion/blob/master/packages/hash/src/index.js
2-
3-
const murmur2 = (str) => {
4-
// 'm' and 'r' are mixing constants generated offline.
5-
// They're not really 'magic', they just happen to work well.
6-
7-
// const m = 0x5bd1e995;
8-
// const r = 24;
9-
10-
// Initialize the hash
11-
12-
var h = 0;
13-
14-
// Mix 4 bytes at a time into the hash
15-
16-
var k,
17-
i = 0,
18-
len = str.length;
19-
for (; len >= 4; ++i, len -= 4) {
20-
k =
21-
(str.charCodeAt(i) & 0xff) |
22-
((str.charCodeAt(++i) & 0xff) << 8) |
23-
((str.charCodeAt(++i) & 0xff) << 16) |
24-
((str.charCodeAt(++i) & 0xff) << 24);
25-
26-
k =
27-
/* Math.imul(k, m): */
28-
(k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16);
29-
k ^= /* k >>> r: */ k >>> 24;
30-
31-
h =
32-
/* Math.imul(k, m): */
33-
((k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16)) ^
34-
/* Math.imul(h, m): */
35-
((h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16));
36-
}
37-
38-
// Handle the last few bytes of the input array
39-
40-
switch (len) {
41-
case 3:
42-
h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
43-
case 2:
44-
h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
45-
case 1:
46-
h ^= str.charCodeAt(i) & 0xff;
47-
h =
48-
/* Math.imul(h, m): */
49-
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16);
50-
}
51-
52-
// Do a few final mixes of the hash to ensure the last few
53-
// bytes are well-incorporated.
54-
55-
h ^= h >>> 13;
56-
h =
57-
/* Math.imul(h, m): */
58-
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16);
59-
60-
return ((h ^ (h >>> 15)) >>> 0).toString(36);
61-
}
62-
63-
// Copied from https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/utils/hash.js
64-
1+
// Copied from https://github.com/styled-components/styled-components/blob/3ee575eddf804e4bf460f1cadb5890db7ae2f5cf/packages/styled-components/src/utils/hash.ts#L6
652
const SEED = 5381;
663

674
// When we have separate strings it's useful to run a progressive
@@ -78,11 +15,8 @@ const phash = (h, x) => {
7815
};
7916

8017
// This is a djb2 hashing function
81-
const hash = (x) => {
18+
const hash = x => {
8219
return phash(SEED, x);
8320
};
8421

85-
// switch between readable vs faster & smaller classnames depending on environment
86-
const readableHash = (value) => CSS.escape(value).replace(/\\./g, '_')
87-
88-
export default murmur2
22+
export default hash;

0 commit comments

Comments
 (0)