| 56 | } |
| 57 | |
| 58 | function test(name, value, ignoreResult, prettyFormatOpts) { |
| 59 | const formatted = testCase('prettyFormat() ', () => |
| 60 | prettyFormat(value, prettyFormatOpts), |
| 61 | ); |
| 62 | |
| 63 | const inspected = testCase('util.inspect() ', () => |
| 64 | util.inspect(value, { |
| 65 | depth: null, |
| 66 | showHidden: true, |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | const stringified = testCase('JSON.stringify()', () => |
| 71 | JSON.stringify(value, null, ' '), |
| 72 | ); |
| 73 | |
| 74 | const results = [formatted, inspected, stringified].sort( |
| 75 | (a, b) => a.time - b.time, |
| 76 | ); |
| 77 | |
| 78 | const winner = results[0]; |
| 79 | |
| 80 | for (const [index, item] of results.entries()) { |
| 81 | item.isWinner = index === 0; |
| 82 | item.isLoser = index === results.length - 1; |
| 83 | } |
| 84 | |
| 85 | function log(current) { |
| 86 | let message = current.name; |
| 87 | |
| 88 | if (current.time) { |
| 89 | message += ` - ${formatTime(current.time, -9, 6)}`; |
| 90 | } |
| 91 | if (current.total) { |
| 92 | message += ` - ${formatTime( |
| 93 | current.total / NANOSECONDS, |
| 94 | 0, |
| 95 | )} total (${TIMES_TO_RUN} runs)`; |
| 96 | } |
| 97 | if (current.error) { |
| 98 | message += ` - Error: ${current.error.message}`; |
| 99 | } |
| 100 | |
| 101 | if (!ignoreResult && current.result) { |
| 102 | message += ` - ${JSON.stringify(current.result)}`; |
| 103 | } |
| 104 | |
| 105 | message = ` ${message} `; |
| 106 | |
| 107 | if (current.error) { |
| 108 | message = chalk.dim(message); |
| 109 | } |
| 110 | |
| 111 | const diff = current.time - winner.time; |
| 112 | |
| 113 | if (diff > winner.time * 0.85) { |
| 114 | message = chalk.bgRed.black(message); |
| 115 | } else if (diff > winner.time * 0.65) { |