aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulian Schurhammer <julian.schurhammer@gmail.com>2022-08-14 22:46:39 +1200
committerLouis Pilfold <louis@lpil.uk>2023-03-13 10:49:34 +0000
commita3e856bcfc32382c0001bbf51c1f4e14acc21bda (patch)
treec534130d12c4d13d207894bf575779d6e5b6b2b7 /src
parent3522d9a57a7ce8c54f57772a89e9ac7dbd34b81f (diff)
downloadgleam_stdlib-a3e856bcfc32382c0001bbf51c1f4e14acc21bda.tar.gz
gleam_stdlib-a3e856bcfc32382c0001bbf51c1f4e14acc21bda.zip
better number hash
Diffstat (limited to 'src')
-rw-r--r--src/persistent-hash-map.mjs8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/persistent-hash-map.mjs b/src/persistent-hash-map.mjs
index 9236ec0..a1e1e3f 100644
--- a/src/persistent-hash-map.mjs
+++ b/src/persistent-hash-map.mjs
@@ -1,6 +1,7 @@
import { isEqual } from "./gleam.mjs";
const referenceMap = new WeakMap();
+const tempDataView = new DataView(new ArrayBuffer(8));
let referenceUID = 0;
/** hash the object by reference using a weak map and incrementing uid */
@@ -29,9 +30,12 @@ function hashString(s) {
}
return hash;
}
-/** convert number to string and hash, seems to be better and faster than anything else */
+/** hash a number by converting to two integers and do some jumbling */
function hashNumber(n) {
- return hashString(n.toString());
+ tempDataView.setFloat64(0, n);
+ const i = tempDataView.getInt32(0);
+ const j = tempDataView.getInt32(4);
+ return Math.imul(0x45d9f3b, (i >> 16) ^ i) ^ j;
}
/** hash any js object */
function hashObject(o) {