Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
digestTwoHashObjects: extract output from uint8OutputArray
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 10, 2021
1 parent b298602 commit b2a0900
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
15 changes: 4 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default class SHA256 {
this.uint8InputArray = new Uint8Array(this.ctx.memory.buffer, this.wasmInputValue, this.ctx.INPUT_LENGTH);
this.uint8OutputArray = new Uint8Array(this.ctx.memory.buffer, this.wasmOutputValue, 32);
this.uint32InputArray = new Uint32Array(this.ctx.memory.buffer, this.wasmInputValue, this.ctx.INPUT_LENGTH);
this.uint32OutputArray = new Uint32Array(this.ctx.memory.buffer, this.wasmOutputValue, 32);
// extracting numbers from Uint32Array causes more memory
// this.uint32OutputArray = new Uint32Array(this.ctx.memory.buffer, this.wasmOutputValue, 32);
}
init() {
this.ctx.init();
Expand Down Expand Up @@ -85,16 +86,8 @@ export default class SHA256 {

staticInstance.ctx.digest64(staticInstance.wasmInputValue, staticInstance.wasmOutputValue);

return {
h0: staticInstance.uint32OutputArray[0],
h1: staticInstance.uint32OutputArray[1],
h2: staticInstance.uint32OutputArray[2],
h3: staticInstance.uint32OutputArray[3],
h4: staticInstance.uint32OutputArray[4],
h5: staticInstance.uint32OutputArray[5],
h6: staticInstance.uint32OutputArray[6],
h7: staticInstance.uint32OutputArray[7],
};
// extracting numbers from Uint32Array causes more memory
return byteArrayToHashObject(staticInstance.uint8OutputArray);
}
}

Expand Down
15 changes: 13 additions & 2 deletions test/benchmark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const sha256 = require("../lib/index");

// As of Aug 05 2021
// hash
// ✓ digestTwoHashObjects 50023 times 18.56678 ops/s 53.85965 ms/op - 1114 runs 60.0 s
// ✓ digest64 50023 times 17.20804 ops/s 58.11236 ms/op - 1033 runs 60.0 s
// ✓ digestTwoHashObjects 50023 times 17.64902 ops/s 56.66037 ms/op - 1059 runs 60.0 s
// ✓ digest64 50023 times 16.99958 ops/s 58.82497 ms/op - 1020 runs 60.0 s
// ✓ hashObjectToByteArray 50023 times 646.1493 ops/s 1.547630 ms/op - 38649 runs 60.0 s
// ✓ byteArrayToHashObject 50023 times 554.0949 ops/s 1.804745 ms/op - 33179 runs 60.0 s

describe("hash", () => {
setBenchOpts({
Expand All @@ -30,6 +32,15 @@ describe("hash", () => {
for (let j = 0; j < iterations; j++) sha256.default.digest64(input);
});

itBench(`hashObjectToByteArray ${iterations} times`, () => {
const byteArr = new Uint8Array(32);
for (let j = 0; j < iterations; j++) sha256.hashObjectToByteArray(obj1, byteArr, 0);
});

itBench(`byteArrayToHashObject ${iterations} times`, () => {
for (let j = 0; j < iterations; j++) sha256.byteArrayToHashObject(buffer1);
});

});

/**
Expand Down

0 comments on commit b2a0900

Please sign in to comment.