From 424eeb97abaa3b2e13ba4f9862913d168d5196d3 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Thu, 3 Sep 2026 22:03:23 +0200 Subject: [PATCH] perf: use crypto.hash when available --- index.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 2a585c9..8b10b77 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,33 @@ var Stats = require('fs').Stats var toString = Object.prototype.toString +// Max size for crypto.hash - beyond this benefits drop to zero or below +var ONESHOT_MAX_LENGTH = 100 * 1024 + +/** + * Compute the base64 SHA1 hash of an entity. + * + * `crypto.hash` is a faster one-shot API, available since Node.js 20.12.0. + * + * @param {Buffer|string} entity + * @return {string} + * @private + */ + +var sha1base64 = typeof crypto.hash === 'function' + ? oneshothash + : streaminghash + +function oneshothash (entity) { + return entity.length <= ONESHOT_MAX_LENGTH + ? crypto.hash('sha1', entity, 'base64') + : streaminghash(entity) +} + +function streaminghash (entity) { + return crypto.createHash('sha1').update(entity, 'utf8').digest('base64') +} + /** * Generate an entity tag. * @@ -43,11 +70,7 @@ function entitytag (entity) { } // compute hash of entity - var hash = crypto - .createHash('sha1') - .update(entity, 'utf8') - .digest('base64') - .substring(0, 27) + var hash = sha1base64(entity).substring(0, 27) // compute length of entity var len = typeof entity === 'string'