Daninet / hash-wasm

Lightning fast hash functions using hand-tuned WebAssembly binaries
https://npmjs.com/package/hash-wasm
Other
894 stars 50 forks source link

Optional IHasher.calculate and Keccak implementation #52

Closed hsk81 closed 1 year ago

hsk81 commented 1 year ago

The Hash_Calculate WASM function seems to be faster, because of less of an overhead due to fewer context switches. Hence, I've exposed it as the IHasher.calculate function:

export type IHasher = {
  /**
   * Shorthand for init . update . digest('binary'|'hex')
   */
  calculate?: {
    (outputType: 'binary'): (data: IDataType) => Uint8Array;
    (outputType?: 'hex'): (data: IDataType) => string;
  }
}

It's optional, since I was too lazy to implement it for all hashing functions, except for keccak. Further, it returns based on the outputType the actual hasher function to avoid an if-switch on the "hot" path during frequent hashing. For example:

const keccak_hasher = await createKeccak(256);
let keccak: (array: Uint8Array) => Uint8Array;
keccak = keccak_hasher.calculate('binary');
Daninet commented 1 year ago

How much faster it is?

hsk81 commented 1 year ago

Maybe 10%

On Tue, Oct 24, 2023, 21:26 Dani Biró @.***> wrote:

How much faster it is?

— Reply to this email directly, view it on GitHub https://github.com/Daninet/hash-wasm/pull/52#issuecomment-1777796885, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHYHBOBW674J2X3IZTJTXTYBAB7BAVCNFSM6AAAAAA6N75U7SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZXG44TMOBYGU . You are receiving this because you authored the thread.Message ID: @.***>