diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 08f627db8867d8..db54e232aa8a5b 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1200,9 +1200,10 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) { output[i] = formatValue(ctx, entries[i], recurseTimes); } ctx.indentationLvl -= 2; - if (state === kWeak) { + if (state === kWeak && !ctx.sorted) { // Sort all entries to have a halfway reliable output (if more entries than - // retrieved ones exist, we can not reliably return the same output). + // retrieved ones exist, we can not reliably return the same output) if the + // output is not sorted anyway. output = output.sort(); } const remaining = entries.length - maxLength; @@ -1227,9 +1228,11 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) { output[i] = `${formatValue(ctx, entries[pos], recurseTimes)}` + ` => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`; } - // Sort all entries to have a halfway reliable output (if more entries - // than retrieved ones exist, we can not reliably return the same output). - output = output.sort(); + // Sort all entries to have a halfway reliable output (if more entries than + // retrieved ones exist, we can not reliably return the same output) if the + // output is not sorted anyway. + if (!ctx.sorted) + output = output.sort(); } else { for (; i < maxLength; i++) { const pos = i * 2;