Skip to content

Commit bdfee10

Browse files
committed
util: optimize promise introspection
Use V8's builtin ObjectIsPromise() to check that the value is a promise before creating the promise mirror. Reduces garbage collector strain in the (common) non-promise case, which is beneficial when inspecting deep object graphs.
1 parent 0e4b772 commit bdfee10

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/util.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
const uv = process.binding('uv');
44
const Buffer = require('buffer').Buffer;
55
const internalUtil = require('internal/util');
6+
67
var Debug;
8+
var ObjectIsPromise;
79

810
const formatRegExp = /%[sdj%]/g;
911
exports.format = function(f) {
@@ -184,10 +186,15 @@ function getConstructorOf(obj) {
184186

185187

186188
function inspectPromise(p) {
187-
Debug = Debug || require('vm').runInDebugContext('Debug');
188-
var mirror = Debug.MakeMirror(p, true);
189-
if (!mirror.isPromise())
189+
if (!Debug) {
190+
const runInDebugContext = require('vm').runInDebugContext;
191+
const result = runInDebugContext('[Debug, ObjectIsPromise]');
192+
Debug = result[0];
193+
ObjectIsPromise = result[1];
194+
}
195+
if (!ObjectIsPromise(p))
190196
return null;
197+
const mirror = Debug.MakeMirror(p, true);
191198
return {status: mirror.status(), value: mirror.promiseValue().value_};
192199
}
193200

0 commit comments

Comments
 (0)