Skip to content

Commit

Permalink
in-memory memoize hash
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroby0 committed Aug 6, 2021
1 parent b0ee0a9 commit 27a1a2b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion img.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 = {}) {
Expand Down

0 comments on commit 27a1a2b

Please sign in to comment.