1'use strict'; 2const util = require('util'); 3 4const common = require('../common.js'); 5 6const opts = { 7 showHidden: { showHidden: true }, 8 colors: { colors: true }, 9 none: undefined 10}; 11const bench = common.createBenchmark(main, { 12 n: [2e4], 13 method: [ 14 'Object', 15 'Object_empty', 16 'Object_deep_ln', 17 'String', 18 'String_complex', 19 'String_boxed', 20 'Date', 21 'Set', 22 'Error', 23 'Array', 24 'TypedArray', 25 'TypedArray_extra', 26 'Number', 27 ], 28 option: Object.keys(opts) 29}); 30 31function benchmark(n, obj, options) { 32 bench.start(); 33 for (let i = 0; i < n; i += 1) { 34 util.inspect(obj, options); 35 } 36 bench.end(n); 37} 38 39function main({ method, n, option }) { 40 let obj; 41 const options = opts[option]; 42 switch (method) { 43 case 'Object': 44 benchmark(n, { a: 'a', b: 'b', c: 'c', d: 'd' }, options); 45 break; 46 case 'Object_empty': 47 benchmark(n, {}, options); 48 break; 49 case 'Object_deep_ln': 50 if (options) 51 options.depth = Infinity; 52 obj = { first: 53 { second: 54 { third: 55 { a: 'first', 56 b: 'second', 57 c: 'third', 58 d: 'fourth', 59 e: 'fifth', 60 f: 'sixth', 61 g: 'seventh' } } } }; 62 benchmark(n, obj, options || { depth: Infinity }); 63 break; 64 case 'String': 65 benchmark(n, 'Simple string', options); 66 break; 67 case 'String_complex': 68 benchmark(n, 'This string\nhas to be\tescaped!', options); 69 break; 70 case 'String_boxed': 71 benchmark(n, new String('string'), options); 72 break; 73 case 'Date': 74 benchmark(n, new Date(), options); 75 break; 76 case 'Set': 77 obj = new Set([5, 3]); 78 benchmark(n, obj, options); 79 break; 80 case 'Error': 81 benchmark(n, new Error('error'), options); 82 break; 83 case 'Array': 84 benchmark(n, Array(50).fill().map((_, i) => i), options); 85 break; 86 case 'TypedArray': 87 obj = new Uint8Array(Array(50).fill().map((_, i) => i)); 88 benchmark(n, obj, options); 89 break; 90 case 'TypedArray_extra': 91 obj = new Uint8Array(Array(50).fill().map((_, i) => i)); 92 obj.foo = 'bar'; 93 obj[Symbol('baz')] = 5; 94 benchmark(n, obj, options); 95 break; 96 case 'Number': 97 benchmark(n, 0, options); 98 break; 99 default: 100 throw new Error(`Unsupported method "${method}"`); 101 } 102} 103