improve hashing for longer strings, fix max short string length
This commit is contained in:
parent
dcf3b02aaf
commit
ade40a4edd
|
@ -45,8 +45,8 @@ const compare = (n1, f1, n2, f2) => {
|
|||
};
|
||||
|
||||
for (const size of [
|
||||
1, 2, 4, 8, 10, 20, 40, 60, 80, 100, 200, 1000, 5000, 10000, 20000, 50000,
|
||||
100000, 200000, 500000
|
||||
1, 2, 4, 8, 10, 20, 40, 60, 80, 100, 200, 1000, 5000, 8183, 8184, 8185, 10000,
|
||||
20000, 32768, 32769, 50000, 100000, 200000, 500000
|
||||
]) {
|
||||
const longString = require("crypto").randomBytes(size).toString("hex");
|
||||
const buffer = require("crypto").randomBytes(size * 2);
|
||||
|
|
|
@ -5,6 +5,12 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
// 65536 is the size of a wasm memory page
|
||||
// 64 is the maximum chunk size for every possible wasm hash implementation
|
||||
// 4 is the maximum number of bytes per char for string encoding (max is utf-8)
|
||||
// ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64
|
||||
const MAX_SHORT_STRING = Math.floor((65536 - 64) / 4) & ~3;
|
||||
|
||||
class WasmHash {
|
||||
/**
|
||||
* @param {WebAssembly.Instance} instance wasm instance
|
||||
|
@ -35,12 +41,12 @@ class WasmHash {
|
|||
*/
|
||||
update(data, encoding) {
|
||||
if (typeof data === "string") {
|
||||
if (data.length < 21845) {
|
||||
this._updateWithShortString(data, encoding);
|
||||
return this;
|
||||
} else {
|
||||
data = Buffer.from(data, encoding);
|
||||
while (data.length > MAX_SHORT_STRING) {
|
||||
this._updateWithShortString(data.slice(0, MAX_SHORT_STRING), encoding);
|
||||
data = data.slice(MAX_SHORT_STRING);
|
||||
}
|
||||
this._updateWithShortString(data, encoding);
|
||||
return this;
|
||||
}
|
||||
this._updateWithBuffer(data);
|
||||
return this;
|
||||
|
|
Loading…
Reference in New Issue