|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const common = require('../common.js'); |
4 | | -const { crc32 } = require('zlib'); |
| 4 | +const zlib = require('zlib'); |
5 | 5 |
|
6 | | -// Benchmark crc32 on Buffer and String inputs across sizes. |
| 6 | +// Benchmark crc32 and crc32c on Buffer and String inputs across sizes. |
7 | 7 | // Iteration count is scaled inversely with input length to keep runtime sane. |
8 | 8 | // Example: |
9 | | -// node benchmark/zlib/crc32.js type=buffer len=4096 n=4000000 |
| 9 | +// node benchmark/zlib/crc32.js algorithm=crc32c type=buffer len=4096 n=4000000 |
10 | 10 | // ./out/Release/node benchmark/zlib/crc32.js --test |
11 | 11 |
|
12 | 12 | const bench = common.createBenchmark(main, { |
| 13 | + algorithm: ['crc32', 'crc32c'], |
13 | 14 | type: ['buffer', 'string'], |
14 | 15 | len: [32, 256, 4096, 65536], |
15 | 16 | n: [4e6], |
@@ -28,19 +29,21 @@ function makeAsciiString(size) { |
28 | 29 | return out; |
29 | 30 | } |
30 | 31 |
|
31 | | -function main({ type, len, n }) { |
| 32 | +function main({ algorithm, type, len, n }) { |
| 33 | + const crc = zlib[algorithm]; |
| 34 | + |
32 | 35 | // Scale iterations so that total processed bytes roughly constant around n*4096 bytes. |
33 | 36 | const scale = 4096 / len; |
34 | 37 | const iters = Math.max(1, Math.floor(n * scale)); |
35 | 38 |
|
36 | 39 | const data = type === 'buffer' ? makeBuffer(len) : makeAsciiString(len); |
37 | 40 |
|
38 | 41 | let acc = 0; |
39 | | - for (let i = 0; i < Math.min(iters, 10000); i++) acc ^= crc32(data, 0); |
| 42 | + for (let i = 0; i < Math.min(iters, 10000); i++) acc ^= crc(data, 0); |
40 | 43 |
|
41 | 44 | bench.start(); |
42 | 45 | let sum = 0; |
43 | | - for (let i = 0; i < iters; i++) sum ^= crc32(data, 0); |
| 46 | + for (let i = 0; i < iters; i++) sum ^= crc(data, 0); |
44 | 47 | bench.end(iters); |
45 | 48 |
|
46 | 49 | if (sum === acc - 1) process.stderr.write(''); |
|