|
| 1 | +import rnqc from 'react-native-quick-crypto'; |
| 2 | +import { sha256 } from '@noble/hashes/sha2'; |
| 3 | +// @ts-expect-error - crypto-browserify is not typed |
| 4 | +import browserify from 'crypto-browserify'; |
| 5 | +import type { BenchFn } from '../../types/benchmarks'; |
| 6 | +import { Bench } from 'tinybench'; |
| 7 | + |
| 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 = () => { |
| 24 | + const bench = new Bench({ |
| 25 | + name: 'hash sha256 8MB string', |
| 26 | + time: 10, |
| 27 | + iterations: 1, |
| 28 | + warmupIterations: 0, |
| 29 | + }); |
| 30 | + |
| 31 | + bench |
| 32 | + .add('rnqc', () => { |
| 33 | + const hash = rnqc.createHash('sha256'); |
| 34 | + hash.update(text8MB); |
| 35 | + hash.digest('hex'); |
| 36 | + }) |
| 37 | + .add('@noble/hashes/sha256', () => { |
| 38 | + sha256(text8MB); |
| 39 | + }) |
| 40 | + .add('browserify', () => { |
| 41 | + const hash = browserify.createHash('sha256'); |
| 42 | + hash.update(text8MB); |
| 43 | + hash.digest('hex'); |
| 44 | + }); |
| 45 | + |
| 46 | + return bench; |
| 47 | +}; |
| 48 | + |
| 49 | +const hash_sha256_1mb: BenchFn = () => { |
| 50 | + const bench = new Bench({ |
| 51 | + name: 'hash sha256 1MB string', |
| 52 | + time: 50, |
| 53 | + iterations: 2, |
| 54 | + warmupIterations: 0, |
| 55 | + }); |
| 56 | + |
| 57 | + bench |
| 58 | + .add('rnqc', () => { |
| 59 | + const hash = rnqc.createHash('sha256'); |
| 60 | + hash.update(text1MB); |
| 61 | + hash.digest('hex'); |
| 62 | + }) |
| 63 | + .add('@noble/hashes/sha256', () => { |
| 64 | + sha256(text1MB); |
| 65 | + }) |
| 66 | + .add('browserify', () => { |
| 67 | + const hash = browserify.createHash('sha256'); |
| 68 | + hash.update(text1MB); |
| 69 | + hash.digest('hex'); |
| 70 | + }); |
| 71 | + |
| 72 | + return bench; |
| 73 | +}; |
| 74 | + |
| 75 | +const hash_sha256_100kb: BenchFn = () => { |
| 76 | + const bench = new Bench({ |
| 77 | + name: 'hash sha256 100KB string', |
| 78 | + iterations: 5, |
| 79 | + }); |
| 80 | + |
| 81 | + bench |
| 82 | + .add('rnqc', () => { |
| 83 | + const hash = rnqc.createHash('sha256'); |
| 84 | + hash.update(text100KB); |
| 85 | + hash.digest('hex'); |
| 86 | + }) |
| 87 | + .add('@noble/hashes/sha256', () => { |
| 88 | + sha256(text100KB); |
| 89 | + }) |
| 90 | + .add('browserify', () => { |
| 91 | + const hash = browserify.createHash('sha256'); |
| 92 | + hash.update(text100KB); |
| 93 | + hash.digest('hex'); |
| 94 | + }); |
| 95 | + |
| 96 | + bench.warmupTime = 100; |
| 97 | + return bench; |
| 98 | +}; |
| 99 | + |
| 100 | +export default [hash_sha256_100kb, hash_sha256_1mb, hash_sha256_8mb]; |
0 commit comments