Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: improve inspect performance #20009

Closed

Conversation

BridgeAR
Copy link
Member

This improves a slow code part in util.inspect by directly
retrieving the Symbol.toStringTag and by optimizing some code
paths.

I benchmarked this with #20007 applied.

'use strict';

const util = require('util');

let i = 0;
const obj = {};

while (i++ < 100000) {
  obj[i] = {
    a: new Set([1, {}]),
    b: [],
    c: [1, 2],
    d: true
  };
}

for (const k of Object.keys(obj)) {
  obj[k].obj = obj;
}

console.time('run');
util.inspect(obj);
console.timeEnd('run');

// node$ ./slow t.js
// run: 692.284ms
// run: 681.384ms
// run: 685.455ms

// node$ ./fast t.js
// run: 645.635ms
// run: 640.754ms
// run: 648.319ms
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the util Issues and PRs related to the built-in util module. label Apr 13, 2018
@BridgeAR
Copy link
Member Author

@BridgeAR BridgeAR added the performance Issues and PRs related to the performance of Node.js. label Apr 13, 2018
@jasnell
Copy link
Member

jasnell commented Apr 13, 2018

Can you post the benchmark results?

@BridgeAR
Copy link
Member Author

@jasnell I posted them in the description below the code?

@BridgeAR
Copy link
Member Author

@jasnell
Copy link
Member

jasnell commented Apr 13, 2018

oh, missed that. Is there not an existing benchmark script that works with compare.js? Seeing those results would be more reliable I think.

@BridgeAR
Copy link
Member Author

Just a few runs:

                                                                        confidence improvement accuracy (*)    (**)   (***)
 util/inspect.js option='colors' method='Array' n=100000                         *      2.44 %       ±2.11%  ±3.10%  ±4.73%
 util/inspect.js option='colors' method='Date' n=100000                        ***     12.46 %       ±3.01%  ±4.22%  ±5.95%
 util/inspect.js option='colors' method='Error' n=100000                       ***     19.52 %       ±2.76%  ±3.86%  ±5.44%
 util/inspect.js option='colors' method='Object_deep_ln' n=100000               **      3.17 %       ±1.81%  ±2.58%  ±3.74%
 util/inspect.js option='colors' method='Object_empty' n=100000                 **     22.80 %      ±11.70% ±17.12% ±25.93%
 util/inspect.js option='colors' method='Object' n=100000                        *      6.63 %       ±6.01%  ±8.62% ±12.65%
 util/inspect.js option='colors' method='Set' n=100000                                  2.63 %       ±2.85%  ±3.98%  ±5.59%
 util/inspect.js option='colors' method='String_boxed' n=100000                ***     12.94 %       ±3.28%  ±4.67%  ±6.74%
 util/inspect.js option='colors' method='String_complex' n=100000                       3.83 %       ±4.73%  ±6.66%  ±9.49%
 util/inspect.js option='colors' method='TypedArray_extra' n=100000                     0.73 %       ±2.67%  ±3.72%  ±5.23%
 util/inspect.js option='colors' method='TypedArray' n=100000                    *      4.21 %       ±3.80%  ±5.57%  ±8.49%
 util/inspect.js option='none' method='Array' n=100000                         ***      4.72 %       ±2.03%  ±2.90%  ±4.22%
 util/inspect.js option='none' method='Date' n=100000                          ***     15.46 %       ±2.86%  ±4.25%  ±6.59%
 util/inspect.js option='none' method='Error' n=100000                         ***     23.22 %       ±2.73%  ±3.82%  ±5.38%
 util/inspect.js option='none' method='Object_deep_ln' n=100000                ***      7.70 %       ±2.91%  ±4.19%  ±6.21%
 util/inspect.js option='none' method='Object_empty' n=100000                   **     16.26 %       ±9.35% ±13.45% ±19.83%
 util/inspect.js option='none' method='Object' n=100000                          *     14.39 %      ±11.48% ±16.93% ±26.01%
 util/inspect.js option='none' method='Set' n=100000                           ***      7.50 %       ±2.50%  ±3.50%  ±4.95%
 util/inspect.js option='none' method='String_boxed' n=100000                  ***     11.00 %       ±3.54%  ±5.04%  ±7.33%
 util/inspect.js option='none' method='String_complex' n=100000                         0.61 %       ±1.59%  ±2.23%  ±3.15%
 util/inspect.js option='none' method='TypedArray_extra' n=100000                       1.87 %       ±2.54%  ±3.55%  ±5.00%
 util/inspect.js option='none' method='TypedArray' n=100000                     **      7.09 %       ±4.82%  ±7.01% ±10.52%
 util/inspect.js option='showHidden' method='Array' n=100000                   ***      3.04 %       ±1.23%  ±1.74%  ±2.50%
 util/inspect.js option='showHidden' method='Date' n=100000                    ***     15.47 %       ±2.79%  ±3.90%  ±5.48%
 util/inspect.js option='showHidden' method='Error' n=100000                     *      2.97 %       ±2.51%  ±3.54%  ±5.05%
 util/inspect.js option='showHidden' method='Object_deep_ln' n=100000            *     11.97 %       ±8.53% ±12.58% ±19.30%
 util/inspect.js option='showHidden' method='Object_empty' n=100000            ***     18.51 %       ±4.08%  ±5.92%  ±8.86%
 util/inspect.js option='showHidden' method='Object' n=100000                  ***      6.28 %       ±2.20%  ±3.05%  ±4.25%
 util/inspect.js option='showHidden' method='Set' n=100000                     ***      9.35 %       ±4.42%  ±6.19%  ±8.73%
 util/inspect.js option='showHidden' method='String_boxed' n=100000            ***      9.79 %       ±2.57%  ±3.64%  ±5.25%
 util/inspect.js option='showHidden' method='String_complex' n=100000                   0.32 %       ±3.38%  ±4.81%  ±6.95%
 util/inspect.js option='showHidden' method='TypedArray_extra' n=100000                 1.49 %       ±1.69%  ±2.36%  ±3.31%
 util/inspect.js option='showHidden' method='TypedArray' n=100000              ***      4.54 %       ±1.78%  ±2.49%  ±3.48%

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 13, 2018
This improves a slow code part in `util.inspect` by directly
retrieving the `Symbol.toStringTag` and by optimizing some code
paths.
@BridgeAR BridgeAR force-pushed the improve-util-inspect-performance branch from 6f1355b to de7e7e7 Compare April 16, 2018 15:39
@BridgeAR
Copy link
Member Author

@jasnell
Copy link
Member

jasnell commented Apr 16, 2018

CI failed. PTAL

@BridgeAR
Copy link
Member Author

@BridgeAR
Copy link
Member Author

Landed in ad1d105

@BridgeAR BridgeAR closed this Apr 19, 2018
BridgeAR added a commit to BridgeAR/node that referenced this pull request Apr 19, 2018
This improves a slow code part in `util.inspect` by directly
retrieving the `Symbol.toStringTag` and by optimizing some code
paths.

PR-URL: nodejs#20009
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
jasnell pushed a commit that referenced this pull request Apr 20, 2018
This improves a slow code part in `util.inspect` by directly
retrieving the `Symbol.toStringTag` and by optimizing some code
paths.

PR-URL: #20009
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@BridgeAR BridgeAR deleted the improve-util-inspect-performance branch April 1, 2019 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. performance Issues and PRs related to the performance of Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants