|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | + |
| 5 | +const bench = common.createBenchmark(main, { |
| 6 | + op: [ |
| 7 | + 'normalizeAlgorithm-string', |
| 8 | + 'normalizeAlgorithm-dict', |
| 9 | + 'webidl-dict', |
| 10 | + 'webidl-algorithm-identifier-string', |
| 11 | + 'webidl-algorithm-identifier-object', |
| 12 | + 'webidl-dict-enforce-range', |
| 13 | + 'webidl-dict-ensure-sha', |
| 14 | + 'webidl-dict-null', |
| 15 | + ], |
| 16 | + n: [1e6], |
| 17 | +}, { flags: ['--expose-internals'] }); |
| 18 | + |
| 19 | +function main({ n, op }) { |
| 20 | + const { normalizeAlgorithm } = require('internal/crypto/util'); |
| 21 | + |
| 22 | + switch (op) { |
| 23 | + case 'normalizeAlgorithm-string': { |
| 24 | + // String shortcut + null dictionary (cheapest path). |
| 25 | + bench.start(); |
| 26 | + for (let i = 0; i < n; i++) |
| 27 | + normalizeAlgorithm('SHA-256', 'digest'); |
| 28 | + bench.end(n); |
| 29 | + break; |
| 30 | + } |
| 31 | + case 'normalizeAlgorithm-dict': { |
| 32 | + // Object input with a dictionary type (no BufferSource members). |
| 33 | + const alg = { name: 'ECDSA', hash: 'SHA-256' }; |
| 34 | + bench.start(); |
| 35 | + for (let i = 0; i < n; i++) |
| 36 | + normalizeAlgorithm(alg, 'sign'); |
| 37 | + bench.end(n); |
| 38 | + break; |
| 39 | + } |
| 40 | + case 'webidl-dict': { |
| 41 | + // WebIDL dictionary converter in isolation. |
| 42 | + const webidl = require('internal/crypto/webidl'); |
| 43 | + const input = { name: 'AES-GCM', iv: new Uint8Array(12) }; |
| 44 | + const opts = { prefix: 'test', context: 'test' }; |
| 45 | + bench.start(); |
| 46 | + for (let i = 0; i < n; i++) |
| 47 | + webidl.converters.AeadParams(input, opts); |
| 48 | + bench.end(n); |
| 49 | + break; |
| 50 | + } |
| 51 | + case 'webidl-algorithm-identifier-string': { |
| 52 | + // Exercises converters.AlgorithmIdentifier string path (isObjectType |
| 53 | + // fast-reject + DOMString conversion). |
| 54 | + const webidl = require('internal/crypto/webidl'); |
| 55 | + const opts = { prefix: 'test', context: 'test' }; |
| 56 | + bench.start(); |
| 57 | + for (let i = 0; i < n; i++) |
| 58 | + webidl.converters.AlgorithmIdentifier('SHA-256', opts); |
| 59 | + bench.end(n); |
| 60 | + break; |
| 61 | + } |
| 62 | + case 'webidl-algorithm-identifier-object': { |
| 63 | + // Exercises converters.AlgorithmIdentifier object path (isObjectType |
| 64 | + // fast-accept, no dictionary walk). |
| 65 | + const webidl = require('internal/crypto/webidl'); |
| 66 | + const input = { name: 'SHA-256' }; |
| 67 | + const opts = { prefix: 'test', context: 'test' }; |
| 68 | + bench.start(); |
| 69 | + for (let i = 0; i < n; i++) |
| 70 | + webidl.converters.AlgorithmIdentifier(input, opts); |
| 71 | + bench.end(n); |
| 72 | + break; |
| 73 | + } |
| 74 | + case 'webidl-dict-enforce-range': { |
| 75 | + // Exercises shared enforceRange integer wrappers (RsaKeyGenParams has |
| 76 | + // both modulusLength: unsigned long and publicExponent: BigInteger). |
| 77 | + const webidl = require('internal/crypto/webidl'); |
| 78 | + const input = { |
| 79 | + name: 'RSASSA-PKCS1-v1_5', |
| 80 | + modulusLength: 2048, |
| 81 | + publicExponent: new Uint8Array([1, 0, 1]), |
| 82 | + }; |
| 83 | + const opts = { prefix: 'test', context: 'test' }; |
| 84 | + bench.start(); |
| 85 | + for (let i = 0; i < n; i++) |
| 86 | + webidl.converters.RsaKeyGenParams(input, opts); |
| 87 | + bench.end(n); |
| 88 | + break; |
| 89 | + } |
| 90 | + case 'webidl-dict-ensure-sha': { |
| 91 | + // Exercises ensureSHA on a hash member (no lowercase allocation). |
| 92 | + const webidl = require('internal/crypto/webidl'); |
| 93 | + const input = { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }; |
| 94 | + const opts = { prefix: 'test', context: 'test' }; |
| 95 | + bench.start(); |
| 96 | + for (let i = 0; i < n; i++) |
| 97 | + webidl.converters.RsaHashedImportParams(input, opts); |
| 98 | + bench.end(n); |
| 99 | + break; |
| 100 | + } |
| 101 | + case 'webidl-dict-null': { |
| 102 | + // Exercises the null/undefined fast path in createDictionaryConverter |
| 103 | + // (JsonWebKey has no required members). |
| 104 | + const webidl = require('internal/crypto/webidl'); |
| 105 | + const opts = { prefix: 'test', context: 'test' }; |
| 106 | + bench.start(); |
| 107 | + for (let i = 0; i < n; i++) |
| 108 | + webidl.converters.JsonWebKey(undefined, opts); |
| 109 | + bench.end(n); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments