@@ -4,28 +4,14 @@ import { sha256 } from '@noble/hashes/sha2';
44import browserify from 'crypto-browserify' ;
55import type { BenchFn } from '../../types/benchmarks' ;
66import { Bench } from 'tinybench' ;
7+ import { text1MB , text8MB , buffer1MB , buffer8MB } from '../testData' ;
78
8- // Generate test data of different sizes using repeating pattern
9- const generateString = ( sizeInMB : number ) : string => {
10- const chunk =
11- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
12- const bytesPerMB = 1024 * 1024 ;
13- const totalBytes = Math . floor ( sizeInMB * bytesPerMB ) ;
14- const repeatCount = Math . ceil ( totalBytes / chunk . length ) ;
15- return chunk . repeat ( repeatCount ) . substring ( 0 , totalBytes ) ;
16- } ;
17-
18- // Pre-generate test data (8MB as reported in issue)
19- const text100KB = generateString ( 0.1 ) ;
20- const text1MB = generateString ( 1 ) ;
21- const text8MB = generateString ( 8 ) ;
22-
23- const hash_sha256_8mb : BenchFn = ( ) => {
9+ const hash_sha256_8mb_string : BenchFn = ( ) => {
2410 const bench = new Bench ( {
2511 name : 'hash sha256 8MB string' ,
26- time : 10 ,
27- iterations : 1 ,
28- warmupIterations : 0 ,
12+ iterations : 3 ,
13+ warmupIterations : 1 ,
14+ time : 0 ,
2915 } ) ;
3016
3117 bench
@@ -46,12 +32,12 @@ const hash_sha256_8mb: BenchFn = () => {
4632 return bench ;
4733} ;
4834
49- const hash_sha256_1mb : BenchFn = ( ) => {
35+ const hash_sha256_1mb_string : BenchFn = ( ) => {
5036 const bench = new Bench ( {
5137 name : 'hash sha256 1MB string' ,
52- time : 50 ,
53- iterations : 2 ,
54- warmupIterations : 0 ,
38+ iterations : 5 ,
39+ warmupIterations : 2 ,
40+ time : 0 ,
5541 } ) ;
5642
5743 bench
@@ -72,29 +58,61 @@ const hash_sha256_1mb: BenchFn = () => {
7258 return bench ;
7359} ;
7460
75- const hash_sha256_100kb : BenchFn = ( ) => {
61+ const hash_sha256_8mb_buffer : BenchFn = ( ) => {
62+ const bench = new Bench ( {
63+ name : 'hash sha256 8MB Buffer' ,
64+ iterations : 3 ,
65+ warmupIterations : 1 ,
66+ time : 0 ,
67+ } ) ;
68+
69+ bench
70+ . add ( 'rnqc' , ( ) => {
71+ const hash = rnqc . createHash ( 'sha256' ) ;
72+ hash . update ( buffer8MB ) ;
73+ hash . digest ( 'hex' ) ;
74+ } )
75+ . add ( '@noble/hashes/sha256' , ( ) => {
76+ sha256 ( buffer8MB ) ;
77+ } )
78+ . add ( 'browserify' , ( ) => {
79+ const hash = browserify . createHash ( 'sha256' ) ;
80+ hash . update ( buffer8MB ) ;
81+ hash . digest ( 'hex' ) ;
82+ } ) ;
83+
84+ return bench ;
85+ } ;
86+
87+ const hash_sha256_1mb_buffer : BenchFn = ( ) => {
7688 const bench = new Bench ( {
77- name : 'hash sha256 100KB string ' ,
89+ name : 'hash sha256 1MB Buffer ' ,
7890 iterations : 5 ,
91+ warmupIterations : 2 ,
92+ time : 0 ,
7993 } ) ;
8094
8195 bench
8296 . add ( 'rnqc' , ( ) => {
8397 const hash = rnqc . createHash ( 'sha256' ) ;
84- hash . update ( text100KB ) ;
98+ hash . update ( buffer1MB ) ;
8599 hash . digest ( 'hex' ) ;
86100 } )
87101 . add ( '@noble/hashes/sha256' , ( ) => {
88- sha256 ( text100KB ) ;
102+ sha256 ( buffer1MB ) ;
89103 } )
90104 . add ( 'browserify' , ( ) => {
91105 const hash = browserify . createHash ( 'sha256' ) ;
92- hash . update ( text100KB ) ;
106+ hash . update ( buffer1MB ) ;
93107 hash . digest ( 'hex' ) ;
94108 } ) ;
95109
96- bench . warmupTime = 100 ;
97110 return bench ;
98111} ;
99112
100- export default [ hash_sha256_100kb , hash_sha256_1mb , hash_sha256_8mb ] ;
113+ export default [
114+ hash_sha256_1mb_string ,
115+ hash_sha256_1mb_buffer ,
116+ hash_sha256_8mb_string ,
117+ hash_sha256_8mb_buffer ,
118+ ] ;
0 commit comments