diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-19 00:55:54 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-07-16 10:46:09 +0200 |
commit | db495896249b29a93d8013b5ee2067ecf87ed081 (patch) | |
tree | fbd6aef6130b6e23535ae48dd285ae73452d69ae /lib/util.js | |
parent | df97126173918ad589c5ceb234204f66d0c5afac (diff) | |
download | android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.tar.gz android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.tar.bz2 android-node-v8-db495896249b29a93d8013b5ee2067ecf87ed081.zip |
console,util: avoid pair array generation in C++
Use a plain `[key, value, key, value]`-style list instead
of an array of pairs for inspecting collections.
This also fixes a bug with `console.table()` where
inspecting a non-key-value `MapIterator` would have
led to odd results.
PR-URL: https://github.com/nodejs/node/pull/20831
Refs: https://github.com/nodejs/node/pull/20719#discussion_r189342513
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/util.js b/lib/util.js index afd3cae724..5413cc0a33 100644 --- a/lib/util.js +++ b/lib/util.js @@ -981,7 +981,7 @@ function formatMap(ctx, value, recurseTimes, keys) { function formatWeakSet(ctx, value, recurseTimes, keys) { const maxArrayLength = Math.max(ctx.maxArrayLength, 0); - const entries = previewEntries(value).slice(0, maxArrayLength + 1); + const [ entries ] = previewEntries(value).slice(0, maxArrayLength + 1); const maxLength = Math.min(maxArrayLength, entries.length); let output = new Array(maxLength); for (var i = 0; i < maxLength; ++i) @@ -998,14 +998,16 @@ function formatWeakSet(ctx, value, recurseTimes, keys) { function formatWeakMap(ctx, value, recurseTimes, keys) { const maxArrayLength = Math.max(ctx.maxArrayLength, 0); - const entries = previewEntries(value).slice(0, maxArrayLength + 1); - const remainder = entries.length > maxArrayLength; - const len = entries.length - (remainder ? 1 : 0); + const [ entries ] = previewEntries(value).slice(0, (maxArrayLength + 1) * 2); + // Entries exist as [key1, val1, key2, val2, ...] + const remainder = entries.length / 2 > maxArrayLength; + const len = entries.length / 2 - (remainder ? 1 : 0); const maxLength = Math.min(maxArrayLength, len); let output = new Array(maxLength); - for (var i = 0; i < len; i++) { - output[i] = `${formatValue(ctx, entries[i][0], recurseTimes)} => ` + - formatValue(ctx, entries[i][1], recurseTimes); + for (var i = 0; i < maxLength; i++) { + const pos = i * 2; + 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). @@ -1017,9 +1019,19 @@ function formatWeakMap(ctx, value, recurseTimes, keys) { return output; } +function zip2(list) { + const ret = Array(list.length / 2); + for (var i = 0; i < ret.length; ++i) + ret[i] = [list[2 * i], list[2 * i + 1]]; + return ret; +} + function formatCollectionIterator(ctx, value, recurseTimes, keys) { const output = []; - for (const entry of previewEntries(value)) { + var [ entries, isKeyValue ] = previewEntries(value); + if (isKeyValue) + entries = zip2(entries); + for (const entry of entries) { if (ctx.maxArrayLength === output.length) { output.push('... more items'); break; |