Skip to content

Commit

Permalink
cache buffer in local var
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 26, 2021
1 parent 223052f commit c382240
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/util/createHash.js
Expand Up @@ -67,25 +67,26 @@ class BulkUpdateDecorator extends Hash {
*/
digest(encoding) {
let digestCache;
const buffer = this.buffer;
if (this.hash === undefined) {
// short data for hash, we can use caching
const cacheKey = `${this.hashKey}-${encoding}`;
digestCache = digestCaches[cacheKey];
if (digestCache === undefined) {
digestCache = digestCaches[cacheKey] = new Map();
}
const cacheEntry = digestCache.get(this.buffer);
const cacheEntry = digestCache.get(buffer);
if (cacheEntry !== undefined) return cacheEntry;
this.hash = this.hashFactory();
}
if (this.buffer.length > 0) {
this.hash.update(this.buffer);
if (buffer.length > 0) {
this.hash.update(buffer);
}
const digestResult = this.hash.digest(encoding);
const result =
typeof digestResult === "string" ? digestResult : digestResult.toString();
if (digestCache !== undefined) {
digestCache.set(this.buffer, result);
digestCache.set(buffer, result);
}
return result;
}
Expand Down

0 comments on commit c382240

Please sign in to comment.