| 15 | }); |
| 16 | |
| 17 | function main({ n, type, double }) { |
| 18 | const { |
| 19 | hideStackFrames, |
| 20 | codes: { |
| 21 | ERR_INVALID_ARG_TYPE, |
| 22 | }, |
| 23 | } = require('internal/errors'); |
| 24 | |
| 25 | const value = 'err'; |
| 26 | |
| 27 | const testfn = (value) => { |
| 28 | if (typeof value !== 'number') { |
| 29 | throw new ERR_INVALID_ARG_TYPE('Benchmark', 'number', value); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | const hideStackFramesTestfn = hideStackFrames((value) => { |
| 34 | if (typeof value !== 'number') { |
| 35 | throw new ERR_INVALID_ARG_TYPE.HideStackFrameError('Benchmark', 'number', value); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | function doubleTestfn(value) { |
| 40 | testfn(value); |
| 41 | } |
| 42 | |
| 43 | const doubleHideStackFramesTestfn = hideStackFrames((value) => { |
| 44 | hideStackFramesTestfn.withoutStackTrace(value); |
| 45 | }); |
| 46 | |
| 47 | const fn = type === 'hide-stackframe' ? |
| 48 | double === 'true' ? |
| 49 | doubleHideStackFramesTestfn : |
| 50 | hideStackFramesTestfn : |
| 51 | double === 'true' ? |
| 52 | doubleTestfn : |
| 53 | testfn; |
| 54 | |
| 55 | const length = 1024; |
| 56 | const array = []; |
| 57 | let errCase = false; |
| 58 | |
| 59 | // Warm up. |
| 60 | for (let i = 0; i < length; ++i) { |
| 61 | try { |
| 62 | fn(value); |
| 63 | } catch (e) { |
| 64 | errCase = true; |
| 65 | array.push(e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bench.start(); |
| 70 | |
| 71 | for (let i = 0; i < n; i++) { |
| 72 | const index = i % length; |
| 73 | try { |
| 74 | fn(value); |