From 27a1a2b61908bd0a525650a59952cb377fe8707b Mon Sep 17 00:00:00 2001 From: Aravind Reddy V Date: Fri, 6 Aug 2021 23:42:32 +0530 Subject: [PATCH] in-memory memoize hash --- img.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/img.js b/img.js index 1dcfeab..4762a05 100644 --- a/img.js +++ b/img.js @@ -122,7 +122,10 @@ function getValidWidths(originalWidth, widths = [], allowUpscale = false) { return filtered.sort((a, b) => a - b); } +let imgHashCache = {}; function getHash(src, imgOptions={}, length=10) { + if(src in imgHashCache) return imgHashCache[src]; + const hash = createHash("sha256"); let opts = Object.assign({ @@ -151,7 +154,9 @@ function getHash(src, imgOptions={}, length=10) { } hash.update(JSON.stringify(opts)); - return hash.digest("base64url").substring(0, length);; + imgHashCache[src] = hash.digest("base64url").substring(0, length); + + return imgHashCache[src]; } function getFilename(src, width, format, options = {}) {