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

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

‎lib/util.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
constuv=process.binding('uv');
44
constBuffer=require('buffer').Buffer;
55
constinternalUtil=require('internal/util');
6+
67
varDebug;
8+
varObjectIsPromise;
79

810
constformatRegExp=/%[sdj%]/g;
911
exports.format=function(f){
@@ -183,11 +185,21 @@ function getConstructorOf(obj) {
183185
}
184186

185187

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

0 commit comments

Comments
 (0)