From 7cdf3464a1db3184c9f635a8fe46a2c829338072 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 2 Aug 2024 13:56:23 +0200 Subject: [PATCH] use raw bitwise instead of buffers --- src/plugins/ircColors/index.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/plugins/ircColors/index.ts b/src/plugins/ircColors/index.ts index 831423fdc..d5cc5f3e4 100644 --- a/src/plugins/ircColors/index.ts +++ b/src/plugins/ircColors/index.ts @@ -21,14 +21,14 @@ import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; // Compute a 64-bit FNV-1a hash of the passed data -function hash(data: ArrayBuffer) { +function hash(id: bigint) { const fnvPrime = 1099511628211n; const offsetBasis = 14695981039346656037n; let result = offsetBasis; - for (const byte of new Uint8Array(data)) { - result ^= BigInt(byte); - result = (result * fnvPrime) % 2n**32n; + for (let i = 7n; i >= 0n; i--) { + result ^= (id >> (8n * i)) & 0xffn; + result = (result * fnvPrime) % 2n ** 32n; } return result; @@ -36,12 +36,7 @@ function hash(data: ArrayBuffer) { // Calculate a CSS color string based on the user ID function calculateNameColorForUser(id: bigint) { - const idBuffer = new ArrayBuffer(8); - { - const idView = new DataView(idBuffer); - idView.setBigUint64(0, id); - } - const idHash = hash(idBuffer); + const idHash = hash(id); return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`; }