GeezerVison is showing github.com — util: use indexed writes in Set and Map inspection · nodejs/node@3e127fd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/util/inspect.js +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | const util = require('util'); | 3 | 3 |
| 4 | 4 | const common = require('../common.js'); | | 5 | +const assert = require('node:assert'); | 5 | 6 |
| 6 | 7 | const opts = { | 7 | 8 | showHidden: { showHidden: true }, | @@ -20,6 +21,8 @@ const bench = common.createBenchmark(main, { | 20 | 21 | 'String_boxed', | 21 | 22 | 'Date', | 22 | 23 | 'Set', | | 24 | + 'Set_large', | | 25 | + 'Map', | 23 | 26 | 'Error', | 24 | 27 | 'Array', | 25 | 28 | 'TypedArray', | @@ -30,11 +33,14 @@ const bench = common.createBenchmark(main, { | 30 | 33 | }); | 31 | 34 |
| 32 | 35 | function benchmark(n, obj, options) { | | 36 | + let output; | 33 | 37 | bench.start(); | 34 | 38 | for (let i = 0; i < n; i += 1) { | 35 | | - util.inspect(obj, options); | | 39 | + output = util.inspect(obj, options); | 36 | 40 | } | 37 | 41 | bench.end(n); | | 42 | + assert.strictEqual(typeof output, 'string'); | | 43 | + assert.notStrictEqual(output.length, 0); | 38 | 44 | } | 39 | 45 |
| 40 | 46 | function main({ method, n, option }) { | @@ -85,6 +91,20 @@ function main({ method, n, option }) { | 85 | 91 | obj = new Set([5, 3]); | 86 | 92 | benchmark(n, obj, options); | 87 | 93 | break; | | 94 | + case 'Set_large': | | 95 | + obj = new Set(); | | 96 | + for (let i = 0; i < 50; i++) { | | 97 | + obj.add(i); | | 98 | + } | | 99 | + benchmark(n, obj, options); | | 100 | + break; | | 101 | + case 'Map': | | 102 | + obj = new Map(); | | 103 | + for (let i = 0; i < 50; i++) { | | 104 | + obj.set(i, i); | | 105 | + } | | 106 | + benchmark(n, obj, options); | | 107 | + break; | 88 | 108 | case 'Error': | 89 | 109 | benchmark(n, new Error('error'), options); | 90 | 110 | break; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2398,11 +2398,11 @@ function formatSet(value, ctx, ignored, recurseTimes) { | 2398 | 2398 | let i = 0; | 2399 | 2399 | for (const v of value) { | 2400 | 2400 | if (i >= maxLength) break; | 2401 | | - ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes)); | | 2401 | + output[output.length] = formatValue(ctx, v, recurseTimes); | 2402 | 2402 | i++; | 2403 | 2403 | } | 2404 | 2404 | if (remaining > 0) { | 2405 | | - ArrayPrototypePush(output, remainingText(remaining)); | | 2405 | + output[output.length] = remainingText(remaining); | 2406 | 2406 | } | 2407 | 2407 | ctx.indentationLvl -= 2; | 2408 | 2408 | return output; | @@ -2417,14 +2417,12 @@ function formatMap(value, ctx, ignored, recurseTimes) { | 2417 | 2417 | let i = 0; | 2418 | 2418 | for (const { 0: k, 1: v } of value) { | 2419 | 2419 | if (i >= maxLength) break; | 2420 | | - ArrayPrototypePush( | 2421 | | - output, | 2422 | | - `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`, | 2423 | | - ); | | 2420 | + output[output.length] = | | 2421 | + `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`; | 2424 | 2422 | i++; | 2425 | 2423 | } | 2426 | 2424 | if (remaining > 0) { | 2427 | | - ArrayPrototypePush(output, remainingText(remaining)); | | 2425 | + output[output.length] = remainingText(remaining); | 2428 | 2426 | } | 2429 | 2427 | ctx.indentationLvl -= 2; | 2430 | 2428 | return output; | |
|