11'use strict' ;
22
33const common = require ( '../common.js' ) ;
4- const { crc32 } = require ( 'zlib' ) ;
4+ const zlib = require ( 'zlib' ) ;
55
6- // Benchmark crc32 on Buffer and String inputs across sizes.
6+ // Benchmark crc32 and crc32c on Buffer and String inputs across sizes.
77// Iteration count is scaled inversely with input length to keep runtime sane.
88// 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
1010// ./out/Release/node benchmark/zlib/crc32.js --test
1111
1212const bench = common . createBenchmark ( main , {
13+ algorithm : [ 'crc32' , 'crc32c' ] ,
1314 type : [ 'buffer' , 'string' ] ,
1415 len : [ 32 , 256 , 4096 , 65536 ] ,
1516 n : [ 4e6 ] ,
@@ -28,19 +29,21 @@ function makeAsciiString(size) {
2829 return out ;
2930}
3031
31- function main ( { type, len, n } ) {
32+ function main ( { algorithm, type, len, n } ) {
33+ const crc = zlib [ algorithm ] ;
34+
3235 // Scale iterations so that total processed bytes roughly constant around n*4096 bytes.
3336 const scale = 4096 / len ;
3437 const iters = Math . max ( 1 , Math . floor ( n * scale ) ) ;
3538
3639 const data = type === 'buffer' ? makeBuffer ( len ) : makeAsciiString ( len ) ;
3740
3841 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 ) ;
4043
4144 bench . start ( ) ;
4245 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 ) ;
4447 bench . end ( iters ) ;
4548
4649 if ( sum === acc - 1 ) process . stderr . write ( '' ) ;
0 commit comments