Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion benchmark/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const util = require('util');

const common = require('../common.js');
const assert = require('node:assert');

const opts = {
showHidden: { showHidden: true },
Expand All @@ -19,6 +20,8 @@ const bench = common.createBenchmark(main, {
'String_boxed',
'Date',
'Set',
'Set_large',
'Map',
'Error',
'Array',
'TypedArray',
Expand All @@ -29,11 +32,14 @@ const bench = common.createBenchmark(main, {
});

function benchmark(n, obj, options) {
let output;
bench.start();
for (let i = 0; i < n; i += 1) {
util.inspect(obj, options);
output = util.inspect(obj, options);
}
bench.end(n);
assert.strictEqual(typeof output, 'string');
assert.notStrictEqual(output.length, 0);
}

function main({ method, n, option }) {
Expand Down Expand Up @@ -77,6 +83,20 @@ function main({ method, n, option }) {
obj = new Set([5, 3]);
benchmark(n, obj, options);
break;
case 'Set_large':
obj = new Set();
for (let i = 0; i < 50; i++) {
obj.add(i);
}
benchmark(n, obj, options);
break;
case 'Map':
obj = new Map();
for (let i = 0; i < 50; i++) {
obj.set(i, i);
}
benchmark(n, obj, options);
break;
case 'Error':
benchmark(n, new Error('error'), options);
break;
Expand Down
12 changes: 5 additions & 7 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2409,11 +2409,11 @@ function formatSet(value, ctx, ignored, recurseTimes) {
let i = 0;
for (const v of value) {
if (i >= maxLength) break;
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
output[output.length] = formatValue(ctx, v, recurseTimes);
i++;
}
if (remaining > 0) {
ArrayPrototypePush(output, remainingText(remaining));
output[output.length] = remainingText(remaining);
}
ctx.indentationLvl -= 2;
return output;
Expand All @@ -2428,14 +2428,12 @@ function formatMap(value, ctx, ignored, recurseTimes) {
let i = 0;
for (const { 0: k, 1: v } of value) {
if (i >= maxLength) break;
ArrayPrototypePush(
output,
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`,
);
output[output.length] =
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`;
i++;
}
if (remaining > 0) {
ArrayPrototypePush(output, remainingText(remaining));
output[output.length] = remainingText(remaining);
}
ctx.indentationLvl -= 2;
return output;
Expand Down
Loading