(fn, options = kEmptyObject)
| 53 | } |
| 54 | |
| 55 | function timerify(fn, options = kEmptyObject) { |
| 56 | validateFunction(fn, 'fn'); |
| 57 | |
| 58 | validateObject(options, 'options'); |
| 59 | const { |
| 60 | histogram, |
| 61 | } = options; |
| 62 | |
| 63 | if (histogram !== undefined && |
| 64 | (!isHistogram(histogram) || typeof histogram.record !== 'function')) { |
| 65 | throw new ERR_INVALID_ARG_TYPE( |
| 66 | 'options.histogram', |
| 67 | 'RecordableHistogram', |
| 68 | histogram); |
| 69 | } |
| 70 | |
| 71 | function timerified(...args) { |
| 72 | const isConstructorCall = new.target !== undefined; |
| 73 | const start = now(); |
| 74 | const result = isConstructorCall ? |
| 75 | ReflectConstruct(fn, args, fn) : |
| 76 | ReflectApply(fn, this, args); |
| 77 | if (!isConstructorCall && typeof result?.finally === 'function') { |
| 78 | return result.finally( |
| 79 | FunctionPrototypeBind( |
| 80 | processComplete, |
| 81 | result, |
| 82 | fn.name, |
| 83 | start, |
| 84 | args, |
| 85 | histogram)); |
| 86 | } |
| 87 | processComplete(fn.name, start, args, histogram); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | ObjectDefineProperties(timerified, { |
| 92 | length: { |
| 93 | __proto__: null, |
| 94 | configurable: false, |
| 95 | enumerable: true, |
| 96 | value: fn.length, |
| 97 | }, |
| 98 | name: { |
| 99 | __proto__: null, |
| 100 | configurable: false, |
| 101 | enumerable: true, |
| 102 | value: `timerified ${fn.name}`, |
| 103 | }, |
| 104 | }); |
| 105 | |
| 106 | return timerified; |
| 107 | } |
| 108 | |
| 109 | module.exports = timerify; |
no test coverage detected
searching dependent graphs…