Skip to content

Commit e2ccae9

Browse files
committed
util: use indexed writes for inspected object properties
Avoid the cost of primordial push calls for each formatted property. Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
1 parent fca40eb commit e2ccae9

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎benchmark/util/inspect.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const bench = common.createBenchmark(main, {
1414
'Object',
1515
'Object_empty',
1616
'Object_deep_ln',
17+
'Object_large',
1718
'String',
1819
'String_complex',
1920
'String_boxed',
@@ -61,6 +62,13 @@ function main({ method, n, option }) {
6162
g: 'seventh' } } } };
6263
benchmark(n, obj, options || { depth: Infinity });
6364
break;
65+
case 'Object_large':
66+
obj = {};
67+
for (let i = 0; i < 50; i++) {
68+
obj[`key${i}`] = i;
69+
}
70+
benchmark(n, obj, options);
71+
break;
6472
case 'String':
6573
benchmark(n, 'Simple string', options);
6674
break;

‎lib/internal/util/inspect.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,14 +1434,12 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
14341434
const tempValue = { [extraKeys[i]]: value.buffer[extraKeys[i]] };
14351435
formatted = formatExtraProperties(ctx, tempValue, recurseTimes, extraKeys[i], typedArray);
14361436
}
1437-
ArrayPrototypePush(output, formatted);
1437+
output[output.length] = formatted;
14381438
}
14391439
}
14401440
for (i = 0; i < keys.length; i++) {
1441-
ArrayPrototypePush(
1442-
output,
1443-
formatProperty(ctx, value, recurseTimes, keys[i], extrasType),
1444-
);
1441+
output[output.length] =
1442+
formatProperty(ctx, value, recurseTimes, keys[i], extrasType);
14451443
}
14461444
if (protoProps !== undefined) {
14471445
ArrayPrototypePushApply(output, protoProps);

0 commit comments

Comments
 (0)