From a3e856bcfc32382c0001bbf51c1f4e14acc21bda Mon Sep 17 00:00:00 2001 From: Julian Schurhammer Date: Sun, 14 Aug 2022 22:46:39 +1200 Subject: better number hash --- src/persistent-hash-map.mjs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src') 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) { -- cgit v1.2.3