From 90e958aa4d225ff1174abea3547da9f7581e0b72 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 2 Apr 2019 07:59:44 +0200 Subject: [PATCH] util: only sort weak entries once This makes sure weak entries are only sorted once, while using the sorted option. PR-URL: https://github.com/nodejs/node/pull/27052 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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;