Skip to content

Commit d8ab4e1

Browse files
bnoordhuisrvagg
authored andcommitted
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. PR-URL: #3130 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent a0b35bf commit d8ab4e1

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/util.js

Lines changed: 15 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) {
@@ -183,11 +185,21 @@ function getConstructorOf(obj) {
183185
}
184186

185187

188+
function ensureDebugIsInitialized() {
189+
if (Debug === undefined) {
190+
const runInDebugContext = require('vm').runInDebugContext;
191+
const result = runInDebugContext('[Debug, ObjectIsPromise]');
192+
Debug = result[0];
193+
ObjectIsPromise = result[1];
194+
}
195+
}
196+
197+
186198
function inspectPromise(p) {
187-
Debug = Debug || require('vm').runInDebugContext('Debug');
188-
var mirror = Debug.MakeMirror(p, true);
189-
if (!mirror.isPromise())
199+
ensureDebugIsInitialized();
200+
if (!ObjectIsPromise(p))
190201
return null;
202+
const mirror = Debug.MakeMirror(p, true);
191203
return {status: mirror.status(), value: mirror.promiseValue().value_};
192204
}
193205

0 commit comments

Comments
 (0)