mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
util: use indexed writes in Set and Map inspection
Avoid the cost of primordial push calls for each formatted entry. Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> PR-URL: https://github.com/nodejs/node/pull/67295 Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
189e0f1d21
commit
4df7015560
2 files changed
+26
-8
No files matched your search
@@ -2,6 +2,7 @@
|
||||
const util = require('util');
|
||||
|
||||
const common = require('../common.js');
|
||||
const assert = require('node:assert');
|
||||
|
||||
const opts = {
|
||||
showHidden: { showHidden: true },
|
||||
@@ -20,6 +21,8 @@ const bench = common.createBenchmark(main, {
|
||||
'String_boxed',
|
||||
'Date',
|
||||
'Set',
|
||||
'Set_large',
|
||||
'Map',
|
||||
'Error',
|
||||
'Array',
|
||||
'TypedArray',
|
||||
@@ -30,11 +33,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 }) {
|
||||
@@ -85,6 +91,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;
|
||||
|
||||
@@ -2398,11 +2398,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;
|
||||
@@ -2417,14 +2417,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;
|
||||
|
||||
Reference in new issue
Block a user