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: https://github.com/nodejs/node/pull/3130 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
3f62c401ff
commit
9a1bc4ea7e
18
lib/util.js
18
lib/util.js
@ -3,7 +3,9 @@
|
||||
const uv = process.binding('uv');
|
||||
const Buffer = require('buffer').Buffer;
|
||||
const internalUtil = require('internal/util');
|
||||
|
||||
var Debug;
|
||||
var ObjectIsPromise;
|
||||
|
||||
const formatRegExp = /%[sdj%]/g;
|
||||
exports.format = function(f) {
|
||||
@ -183,11 +185,21 @@ function getConstructorOf(obj) {
|
||||
}
|
||||
|
||||
|
||||
function ensureDebugIsInitialized() {
|
||||
if (Debug === undefined) {
|
||||
const runInDebugContext = require('vm').runInDebugContext;
|
||||
const result = runInDebugContext('[Debug, ObjectIsPromise]');
|
||||
Debug = result[0];
|
||||
ObjectIsPromise = result[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function inspectPromise(p) {
|
||||
Debug = Debug || require('vm').runInDebugContext('Debug');
|
||||
var mirror = Debug.MakeMirror(p, true);
|
||||
if (!mirror.isPromise())
|
||||
ensureDebugIsInitialized();
|
||||
if (!ObjectIsPromise(p))
|
||||
return null;
|
||||
const mirror = Debug.MakeMirror(p, true);
|
||||
return {status: mirror.status(), value: mirror.promiseValue().value_};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user