Skip to content

Commit

Permalink
util: optimize promise introspection
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bnoordhuis authored and rvagg committed Oct 2, 2015
1 parent a0b35bf commit d8ab4e1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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_};
}

Expand Down

0 comments on commit d8ab4e1

Please sign in to comment.