util: optimize promise introspection
authorBen Noordhuis <info@bnoordhuis.nl>
Wed, 30 Sep 2015 00:07:56 +0000 (02:07 +0200)
committerRod Vagg <rod@vagg.org>
Fri, 2 Oct 2015 03:39:24 +0000 (13:39 +1000)
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>
lib/util.js

index c5d7bea..7b2bfd2 100644 (file)
@@ -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_};
 }