From 97a8d865dcaf5e536b3f13338a26b73f831686d4 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 27 Sep 2021 13:54:28 +0800 Subject: [PATCH] WebUI: revise hash function In benchmark, using `Math.imul()` is about ~20% faster than floating point multiplication. PR #15475. --- src/webui/www/private/scripts/client.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index a5216b6e2..a0b3e877d 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -88,11 +88,12 @@ const loadSelectedTracker = function() { loadSelectedTracker(); function genHash(string) { + // origins: + // https://stackoverflow.com/a/8831937 + // https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0 let hash = 0; - for (let i = 0; i < string.length; ++i) { - const c = string.charCodeAt(i); - hash = (c + hash * 31) | 0; - } + for (let i = 0; i < string.length; ++i) + hash = ((Math.imul(hash, 31) + string.charCodeAt(i)) | 0); return hash; }