Skip to content

Commit

Permalink
benchmark against node:zlib.crc32 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe committed Oct 20, 2024
1 parent fc79b0d commit 1eebdd4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions benchmarks/crc.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const bench = require('micro-bmark');
const crc32 = require('..');
const zlib = require('node:zlib');

const smallBuffer = Buffer.from([0, 1, 2, 3, 4, 5]);
const largeBuffer = Buffer.alloc(100000);

// Do a few iterations first to get insight into the performance before JIT optimization.
bench.mark(' few iterations, small buffer, native', 10, () => zlib.crc32(smallBuffer));
bench.mark(' few iterations, small buffer, js ', 10, () => crc32.unsigned(smallBuffer));
bench.mark(' few iterations, large buffer, native', 10, () => zlib.crc32(largeBuffer));
bench.mark(' few iterations, large buffer, js ', 10, () => crc32.unsigned(largeBuffer));

// Run many iterations to warm up the JIT optimization.
bench.mark('(warm up. ignore)', 10000, () => zlib.crc32(smallBuffer));
bench.mark('(warm up. ignore)', 10000, () => crc32.unsigned(smallBuffer));
bench.mark('(warm up. ignore)', 10000, () => zlib.crc32(largeBuffer));
bench.mark('(warm up. ignore)', 10000, () => crc32.unsigned(largeBuffer));

// Then collect data once the optimization has presumably reached a steady state.
bench.mark('many iterations, small buffer, native', 20000, () => zlib.crc32(smallBuffer));
bench.mark('many iterations, small buffer, js ', 20000, () => crc32.unsigned(smallBuffer));
bench.mark('many iterations, large buffer, native', 20000, () => zlib.crc32(largeBuffer));
bench.mark('many iterations, large buffer, js ', 20000, () => crc32.unsigned(largeBuffer));
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
},
"scripts": {
"test": "tap tests/*.test.js --reporter classic",
"benchmark": "node benchmarks/crc.bench.js",
"build": "npx [email protected] && npx cpy-cli index.d.ts dist --rename=index.d.cts && npx cpy-cli index.d.ts dist --rename=index.d.mts",
"prepublishOnly": "npm run build",
"format": "prettier --write --log-level warn \"**/*.{json,md,js}\""
},
"dependencies": {},
"devDependencies": {
"micro-bmark": "^0.3.1",
"prettier": "^3.2.4",
"tap": "~11.1.5"
},
Expand Down

0 comments on commit 1eebdd4

Please sign in to comment.