diff --git a/HISTORY.md b/HISTORY.md index 3846cf0..167fafb 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Slightly improve speed for weak ETags over 1KB + 1.4.0 / 2014-09-21 ================== diff --git a/README.md b/README.md index d770f49..16aaa20 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ $ npm test ```bash $ npm run-script bench -> etag@1.2.0 bench nodejs-etag +> etag@1.5.0-pre bench nodejs-etag > node benchmark/index.js > node benchmark/body0-100b.js @@ -63,10 +63,10 @@ $ npm run-script bench 3 tests completed. 4 tests completed. - buffer - strong x 518,895 ops/sec ±1.71% (185 runs sampled) -* buffer - weak x 1,917,975 ops/sec ±0.34% (195 runs sampled) - string - strong x 245,251 ops/sec ±0.90% (190 runs sampled) - string - weak x 442,232 ops/sec ±0.21% (196 runs sampled) + buffer - strong x 426,717 ops/sec ±1.34% (181 runs sampled) +* buffer - weak x 1,081,596 ops/sec ±0.32% (196 runs sampled) + string - strong x 235,880 ops/sec ±1.01% (190 runs sampled) + string - weak x 373,234 ops/sec ±0.87% (192 runs sampled) > node benchmark/body1-1kb.js @@ -77,10 +77,10 @@ $ npm run-script bench 3 tests completed. 4 tests completed. - buffer - strong x 309,748 ops/sec ±0.99% (191 runs sampled) -* buffer - weak x 352,402 ops/sec ±0.20% (198 runs sampled) - string - strong x 159,058 ops/sec ±1.83% (191 runs sampled) - string - weak x 184,052 ops/sec ±1.30% (189 runs sampled) + buffer - strong x 274,188 ops/sec ±1.17% (192 runs sampled) +* buffer - weak x 298,451 ops/sec ±0.49% (194 runs sampled) + string - strong x 157,331 ops/sec ±2.12% (186 runs sampled) + string - weak x 169,242 ops/sec ±1.51% (188 runs sampled) > node benchmark/body2-5kb.js @@ -91,10 +91,10 @@ $ npm run-script bench 3 tests completed. 4 tests completed. -* buffer - strong x 110,157 ops/sec ±0.60% (194 runs sampled) -* buffer - weak x 111,333 ops/sec ±0.67% (194 runs sampled) - string - strong x 62,091 ops/sec ±3.92% (186 runs sampled) - string - weak x 60,681 ops/sec ±3.98% (186 runs sampled) + buffer - strong x 102,624 ops/sec ±0.93% (193 runs sampled) +* buffer - weak x 104,696 ops/sec ±1.17% (190 runs sampled) + string - strong x 59,097 ops/sec ±3.93% (186 runs sampled) + string - weak x 59,202 ops/sec ±4.01% (185 runs sampled) > node benchmark/body3-10kb.js @@ -105,10 +105,10 @@ $ npm run-script bench 3 tests completed. 4 tests completed. -* buffer - strong x 61,843 ops/sec ±0.44% (197 runs sampled) -* buffer - weak x 61,687 ops/sec ±0.52% (197 runs sampled) - string - strong x 41,377 ops/sec ±3.33% (189 runs sampled) - string - weak x 41,368 ops/sec ±3.29% (190 runs sampled) + buffer - strong x 54,768 ops/sec ±1.43% (188 runs sampled) +* buffer - weak x 57,393 ops/sec ±1.10% (192 runs sampled) + string - strong x 36,597 ops/sec ±3.81% (179 runs sampled) + string - weak x 35,525 ops/sec ±3.82% (186 runs sampled) > node benchmark/body4-100kb.js @@ -119,10 +119,10 @@ $ npm run-script bench 3 tests completed. 4 tests completed. -* buffer - strong x 6,874 ops/sec ±0.17% (198 runs sampled) -* buffer - weak x 6,880 ops/sec ±0.15% (198 runs sampled) - string - strong x 5,382 ops/sec ±2.17% (192 runs sampled) - string - weak x 5,361 ops/sec ±2.23% (192 runs sampled) +* buffer - strong x 6,243 ops/sec ±0.84% (194 runs sampled) +* buffer - weak x 6,312 ops/sec ±0.95% (193 runs sampled) + string - strong x 4,984 ops/sec ±2.21% (191 runs sampled) + string - weak x 5,068 ops/sec ±2.32% (190 runs sampled) ``` ## License diff --git a/index.js b/index.js index 3366af8..ce9787a 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ function etag(entity, options) { var buf = !isBuffer ? new Buffer(entity, 'utf8') : entity - var hash = weak && buf.length <= crc32threshold + var hash = weak ? weakhash(buf) : stronghash(buf) @@ -157,5 +157,14 @@ function weakhash(buf) { return '0-0' } - return buf.length.toString(16) + '-' + crc(buf).toString(16) + if (buf.length <= crc32threshold) { + // crc32 plus length when it's fast + return buf.length.toString(16) + '-' + crc(buf).toString(16) + } + + // use md4 for long strings + return crypto + .createHash('md4') + .update(buf) + .digest('base64') } diff --git a/test/test.js b/test/test.js index f7e8121..0708117 100644 --- a/test/test.js +++ b/test/test.js @@ -86,13 +86,13 @@ describe('etag(entity)', function () { it('should generate a weak ETag for a string', function () { assert.equal(etag('', {weak: true}), 'W/"0-0"') assert.equal(etag('beep boop', {weak: true}), 'W/"9-7f3ee715"') - assert.equal(etag(str5kb, {weak: true}), 'W/"8Kq68cJq4i+5US7RLWrE1g=="') + assert.equal(etag(str5kb, {weak: true}), 'W/"3Ikd9BFqLeTIltAti5IvKg=="') }) it('should generate a weak ETag for a Buffer', function () { assert.equal(etag(new Buffer(0), {weak: true}), 'W/"0-0"') assert.equal(etag(new Buffer([1, 2, 3]), {weak: true}), 'W/"3-55bc801d"') - assert.equal(etag(buf5kb, {weak: true}), 'W/"8Kq68cJq4i+5US7RLWrE1g=="') + assert.equal(etag(buf5kb, {weak: true}), 'W/"3Ikd9BFqLeTIltAti5IvKg=="') }) it('should generate a weak ETag for fs.Stats', function () {