Skip to content

Commit

Permalink
chore: add Node.js CRC32 for comparison (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Sep 24, 2024
1 parent 9bd9883 commit 46bddf4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# benchmark-aws-crt-crc32

Benchmark AWS Crypto JS checksum implementation vs ones provided by AWS CRT
Benchmark AWS Crypto JS checksum implementation vs ones provided by AWS CRT vs Node.js built-in.

## Pre-requisites

Expand All @@ -14,22 +14,26 @@ Benchmark AWS Crypto JS checksum implementation vs ones provided by AWS CRT
$ yarn bench

Benchmark for buffer of size 16 KB:
awsCrc32 x 5,807 ops/sec ±1.30% (96 runs sampled)
awsCrtCrc32 x 398,353 ops/sec ±1.37% (92 runs sampled)
Fastest is awsCrtCrc32
awsCrc32 x 5,878 ops/sec ±0.47% (98 runs sampled)
awsCrtCrc32 x 396,066 ops/sec ±1.08% (93 runs sampled)
nodeJsCrc32 x 562,327 ops/sec ±14.53% (83 runs sampled)
Fastest is nodeJsCrc32

Benchmark for buffer of size 64 KB:
awsCrc32 x 1,149 ops/sec ±1.16% (78 runs sampled)
awsCrtCrc32 x 120,999 ops/sec ±0.84% (98 runs sampled)
Fastest is awsCrtCrc32
awsCrc32 x 855 ops/sec ±1.76% (65 runs sampled)
awsCrtCrc32 x 112,400 ops/sec ±1.94% (83 runs sampled)
nodeJsCrc32 x 294,393 ops/sec ±7.50% (89 runs sampled)
Fastest is nodeJsCrc32

Benchmark for buffer of size 256 KB:
awsCrc32 x 337 ops/sec ±0.90% (87 runs sampled)
awsCrtCrc32 x 31,508 ops/sec ±0.82% (96 runs sampled)
Fastest is awsCrtCrc32
awsCrc32 x 278 ops/sec ±3.37% (76 runs sampled)
awsCrtCrc32 x 25,769 ops/sec ±6.58% (80 runs sampled)
nodeJsCrc32 x 92,877 ops/sec ±1.91% (92 runs sampled)
Fastest is nodeJsCrc32

Benchmark for buffer of size 1024 KB:
awsCrc32 x 84.05 ops/sec ±0.53% (73 runs sampled)
awsCrtCrc32 x 8,095 ops/sec ±0.22% (101 runs sampled)
Fastest is awsCrtCrc32
awsCrc32 x 58.60 ops/sec ±6.01% (62 runs sampled)
awsCrtCrc32 x 7,273 ops/sec ±6.00% (89 runs sampled)
nodeJsCrc32 x 22,771 ops/sec ±4.53% (92 runs sampled)
Fastest is nodeJsCrc32
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "benchmark-aws-crt-crc32",
"version": "1.0.0",
"description": "Benchmark AWS Crypto JS checksum implementation vs ones provided by AWS CRT",
"description": "Benchmark AWS Crypto JS checksum implementation vs ones provided by AWS CRT vs Node.js built-in",
"main": "index.js",
"scripts": {
"bench": "node src/bench.js"
Expand Down
21 changes: 21 additions & 0 deletions src/NodeJsCrc32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { crc32 } from "node:zlib";

export class NodeJsCrc32 {
update(toHash) {
this.previous = crc32(toHash, this.previous);
}

async digest() {
// numToUint8 from @aws-crypto/util
return new Uint8Array([
(this.previous & 0xff000000) >> 24,
(this.previous & 0x00ff0000) >> 16,
(this.previous & 0x0000ff00) >> 8,
this.previous & 0x000000ff,
]);
}

reset() {
this.previous = undefined;
}
}
14 changes: 13 additions & 1 deletion src/bench.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import benchmark from "benchmark";
import { AwsCrc32 } from "@aws-crypto/crc32";
import { AwsCrtCrc32 } from "./AwsCrtCrc32.js";
import { NodeJsCrc32 } from "./NodeJsCrc32.js";
import { equal } from "assert";

const generateBuffer = (size) => {
Expand All @@ -11,18 +12,24 @@ const generateBuffer = (size) => {

const awsCrc32 = new AwsCrc32();
const awsCrtCrc32 = new AwsCrtCrc32();
const nodeJsCrc32 = new NodeJsCrc32();

for (const bufferSizeInKB of [16, 64, 256, 1024]) {
const suite = new benchmark.Suite();
const testBuffer = generateBuffer(bufferSizeInKB * 1024);

awsCrc32.update(testBuffer);
awsCrtCrc32.update(testBuffer);
nodeJsCrc32.update(testBuffer);

equal(
(await awsCrc32.digest()).toString(16),
(await awsCrtCrc32.digest()).toString(16)
);
equal(
(await awsCrc32.digest()).toString(16),
(await nodeJsCrc32.digest()).toString(16)
);

console.log(`\nBenchmark for buffer of size ${bufferSizeInKB} KB:`);
suite
Expand All @@ -34,7 +41,12 @@ for (const bufferSizeInKB of [16, 64, 256, 1024]) {
.add("awsCrtCrc32", async () => {
awsCrtCrc32.reset();
awsCrtCrc32.update(testBuffer);
await awsCrtCrc32.digest(16);
await awsCrtCrc32.digest();
})
.add("nodeJsCrc32", async () => {
nodeJsCrc32.reset();
nodeJsCrc32.update(testBuffer);
await nodeJsCrc32.digest();
})
.on("cycle", (event) => {
console.log(String(event.target));
Expand Down

0 comments on commit 46bddf4

Please sign in to comment.