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.
  • Loading branch information
bnoordhuis committed Sep 30, 2015
1 parent 0e4b772 commit bdfee10
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 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 @@ -184,10 +186,15 @@ function getConstructorOf(obj) {


function inspectPromise(p) {
Debug = Debug || require('vm').runInDebugContext('Debug');
var mirror = Debug.MakeMirror(p, true);
if (!mirror.isPromise())
if (!Debug) {
const runInDebugContext = require('vm').runInDebugContext;
const result = runInDebugContext('[Debug, ObjectIsPromise]');
Debug = result[0];
ObjectIsPromise = result[1];
}
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 bdfee10

Please sign in to comment.