|
| 1 | +import rnqc from 'react-native-quick-crypto'; |
| 2 | +// @ts-expect-error - crypto-browserify is not typed |
| 3 | +import browserify from 'crypto-browserify'; |
| 4 | +import { hmac } from '@noble/hashes/hmac'; |
| 5 | +import { sha256 } from '@noble/hashes/sha2'; |
| 6 | +import type { BenchFn } from '../../types/benchmarks'; |
| 7 | +import { Bench } from 'tinybench'; |
| 8 | +import { text1MB, text8MB, buffer1MB, buffer8MB } from '../testData'; |
| 9 | + |
| 10 | +const hmacKey = 'test-key-for-hmac-benchmarks'; |
| 11 | + |
| 12 | +const hmac_sha256_8mb_string: BenchFn = () => { |
| 13 | + const bench = new Bench({ |
| 14 | + name: 'hmac sha256 8MB string', |
| 15 | + iterations: 3, |
| 16 | + warmupIterations: 1, |
| 17 | + time: 0, |
| 18 | + }); |
| 19 | + |
| 20 | + bench |
| 21 | + .add('rnqc', () => { |
| 22 | + const h = rnqc.createHmac('sha256', hmacKey); |
| 23 | + h.update(text8MB); |
| 24 | + h.digest('hex'); |
| 25 | + }) |
| 26 | + .add('@noble/hashes/hmac', () => { |
| 27 | + hmac(sha256, hmacKey, text8MB); |
| 28 | + }) |
| 29 | + .add('browserify', () => { |
| 30 | + const h = browserify.createHmac('sha256', hmacKey); |
| 31 | + h.update(text8MB); |
| 32 | + h.digest('hex'); |
| 33 | + }); |
| 34 | + |
| 35 | + return bench; |
| 36 | +}; |
| 37 | + |
| 38 | +const hmac_sha256_1mb_string: BenchFn = () => { |
| 39 | + const bench = new Bench({ |
| 40 | + name: 'hmac sha256 1MB string', |
| 41 | + iterations: 5, |
| 42 | + warmupIterations: 2, |
| 43 | + time: 0, |
| 44 | + }); |
| 45 | + |
| 46 | + bench |
| 47 | + .add('rnqc', () => { |
| 48 | + const h = rnqc.createHmac('sha256', hmacKey); |
| 49 | + h.update(text1MB); |
| 50 | + h.digest('hex'); |
| 51 | + }) |
| 52 | + .add('@noble/hashes/hmac', () => { |
| 53 | + hmac(sha256, hmacKey, text1MB); |
| 54 | + }) |
| 55 | + .add('browserify', () => { |
| 56 | + const h = browserify.createHmac('sha256', hmacKey); |
| 57 | + h.update(text1MB); |
| 58 | + h.digest('hex'); |
| 59 | + }); |
| 60 | + |
| 61 | + return bench; |
| 62 | +}; |
| 63 | + |
| 64 | +const hmac_sha256_8mb_buffer: BenchFn = () => { |
| 65 | + const bench = new Bench({ |
| 66 | + name: 'hmac sha256 8MB Buffer', |
| 67 | + iterations: 3, |
| 68 | + warmupIterations: 1, |
| 69 | + time: 0, |
| 70 | + }); |
| 71 | + |
| 72 | + bench |
| 73 | + .add('rnqc', () => { |
| 74 | + const h = rnqc.createHmac('sha256', hmacKey); |
| 75 | + h.update(buffer8MB); |
| 76 | + h.digest('hex'); |
| 77 | + }) |
| 78 | + .add('@noble/hashes/hmac', () => { |
| 79 | + hmac(sha256, hmacKey, buffer8MB); |
| 80 | + }) |
| 81 | + .add('browserify', () => { |
| 82 | + const h = browserify.createHmac('sha256', hmacKey); |
| 83 | + h.update(buffer8MB); |
| 84 | + h.digest('hex'); |
| 85 | + }); |
| 86 | + |
| 87 | + return bench; |
| 88 | +}; |
| 89 | + |
| 90 | +const hmac_sha256_1mb_buffer: BenchFn = () => { |
| 91 | + const bench = new Bench({ |
| 92 | + name: 'hmac sha256 1MB Buffer', |
| 93 | + iterations: 5, |
| 94 | + warmupIterations: 2, |
| 95 | + time: 0, |
| 96 | + }); |
| 97 | + |
| 98 | + bench |
| 99 | + .add('rnqc', () => { |
| 100 | + const h = rnqc.createHmac('sha256', hmacKey); |
| 101 | + h.update(buffer1MB); |
| 102 | + h.digest('hex'); |
| 103 | + }) |
| 104 | + .add('@noble/hashes/hmac', () => { |
| 105 | + hmac(sha256, hmacKey, buffer1MB); |
| 106 | + }) |
| 107 | + .add('browserify', () => { |
| 108 | + const h = browserify.createHmac('sha256', hmacKey); |
| 109 | + h.update(buffer1MB); |
| 110 | + h.digest('hex'); |
| 111 | + }); |
| 112 | + |
| 113 | + return bench; |
| 114 | +}; |
| 115 | + |
| 116 | +export default [ |
| 117 | + hmac_sha256_1mb_string, |
| 118 | + hmac_sha256_1mb_buffer, |
| 119 | + hmac_sha256_8mb_string, |
| 120 | + hmac_sha256_8mb_buffer, |
| 121 | +]; |
0 commit comments