-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add minComplexity option to inspect
To limit the maximum computation time this adds a `minComplexity` option. Up to that complexity any object will be fully inspected. As soon as that limit is reached the time is going to be measured for the further inspection and the inspection is limited to the absolute minimum after one second has passed. That makes sure the event loop is not blocked for to long while still producing a good output in almost all cases. To make sure the output is almost deterministic even though a timeout is used, it will only measure the time each 1e5 complexity units. This also limits the performance overhead to the minimum.
- Loading branch information
Showing
3 changed files
with
98 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// `util.inspect` is capable of computing a string that is bigger than 2 ** 28 | ||
// in one second, so let's just skip this test on 32bit systems. | ||
common.skipIf32Bits(); | ||
|
||
// Test that big objects are running only up to the maximum complexity plus ~1 | ||
// second. | ||
|
||
const util = require('util'); | ||
|
||
// Create a difficult to stringify object. Without the limit this would crash. | ||
let last = {}; | ||
const obj = last; | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
last.next = { circular: obj, last, obj: { a: 1, b: 2, c: true } }; | ||
last = last.next; | ||
obj[i] = last; | ||
} | ||
|
||
common.expectWarning( | ||
'Warning', | ||
'util.inspect took to long.', | ||
'INSPECTION_ABORTED' | ||
); | ||
|
||
util.inspect(obj, { depth: Infinity, budget: 1e6 }); |