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 <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-04-02 07:59:44 +02:00
parent 1940114ac3
commit 90e958aa4d
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -1200,9 +1200,10 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
output[i] = formatValue(ctx, entries[i], recurseTimes); output[i] = formatValue(ctx, entries[i], recurseTimes);
} }
ctx.indentationLvl -= 2; ctx.indentationLvl -= 2;
if (state === kWeak) { if (state === kWeak && !ctx.sorted) {
// Sort all entries to have a halfway reliable output (if more entries than // 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(); output = output.sort();
} }
const remaining = entries.length - maxLength; const remaining = entries.length - maxLength;
@ -1227,9 +1228,11 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
output[i] = `${formatValue(ctx, entries[pos], recurseTimes)}` + output[i] = `${formatValue(ctx, entries[pos], recurseTimes)}` +
` => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`; ` => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`;
} }
// Sort all entries to have a halfway reliable output (if more entries // Sort all entries to have a halfway reliable output (if more entries than
// 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 = output.sort(); // output is not sorted anyway.
if (!ctx.sorted)
output = output.sort();
} else { } else {
for (; i < maxLength; i++) { for (; i < maxLength; i++) {
const pos = i * 2; const pos = i * 2;