Skip to content

Commit 016499f

Browse files
committed
util: improve inspect constructor name lookup
This adds known constructors to act as fast path when util.inspect() checks those.
1 parent e785a12 commit 016499f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/internal/util/inspect.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ const {
2020
ArrayPrototypeSplice,
2121
ArrayPrototypeUnshift,
2222
BigIntPrototypeValueOf,
23+
Boolean,
24+
BooleanPrototype,
2325
BooleanPrototypeValueOf,
26+
DataView,
27+
DataViewPrototype,
28+
Date,
29+
DatePrototype,
2430
DatePrototypeGetTime,
2531
DatePrototypeToISOString,
2632
DatePrototypeToString,
33+
Error,
34+
ErrorPrototype,
2735
ErrorPrototypeToString,
2836
Function,
2937
FunctionPrototype,
@@ -47,6 +55,7 @@ const {
4755
NumberIsNaN,
4856
NumberParseFloat,
4957
NumberParseInt,
58+
NumberPrototype,
5059
NumberPrototypeToString,
5160
NumberPrototypeValueOf,
5261
Object,
@@ -63,9 +72,12 @@ const {
6372
ObjectPrototypePropertyIsEnumerable,
6473
ObjectSeal,
6574
ObjectSetPrototypeOf,
75+
Promise,
76+
PromisePrototype,
6677
ReflectApply,
6778
ReflectOwnKeys,
6879
RegExp,
80+
RegExpPrototype,
6981
RegExpPrototypeExec,
7082
RegExpPrototypeSymbolReplace,
7183
RegExpPrototypeSymbolSplit,
@@ -78,6 +90,7 @@ const {
7890
SetPrototypeGetSize,
7991
SetPrototypeValues,
8092
String,
93+
StringPrototype,
8194
StringPrototypeCharCodeAt,
8295
StringPrototypeCodePointAt,
8396
StringPrototypeEndsWith,
@@ -106,6 +119,10 @@ const {
106119
TypedArrayPrototypeGetLength,
107120
TypedArrayPrototypeGetSymbolToStringTag,
108121
Uint8Array,
122+
WeakMap,
123+
WeakMapPrototype,
124+
WeakSet,
125+
WeakSetPrototype,
109126
globalThis,
110127
uncurryThis,
111128
} = primordials;
@@ -613,16 +630,26 @@ wellKnownPrototypes.set(ArrayPrototype, { name: 'Array', constructor: Array });
613630
wellKnownPrototypes.set(ArrayBufferPrototype, { name: 'ArrayBuffer', constructor: ArrayBuffer });
614631
wellKnownPrototypes.set(FunctionPrototype, { name: 'Function', constructor: Function });
615632
wellKnownPrototypes.set(MapPrototype, { name: 'Map', constructor: Map });
616-
wellKnownPrototypes.set(ObjectPrototype, { name: 'Object', constructor: Object });
617633
wellKnownPrototypes.set(SetPrototype, { name: 'Set', constructor: Set });
634+
wellKnownPrototypes.set(ObjectPrototype, { name: 'Object', constructor: Object });
618635
wellKnownPrototypes.set(TypedArrayPrototype, { name: 'TypedArray', constructor: TypedArray });
636+
wellKnownPrototypes.set(RegExpPrototype, { name: 'RegExp', constructor: RegExp });
637+
wellKnownPrototypes.set(DatePrototype, { name: 'Date', constructor: Date });
638+
wellKnownPrototypes.set(DataViewPrototype, { name: 'DataView', constructor: DataView });
639+
wellKnownPrototypes.set(ErrorPrototype, { name: 'Error', constructor: Error });
640+
wellKnownPrototypes.set(BooleanPrototype, { name: 'Boolean', constructor: Boolean });
641+
wellKnownPrototypes.set(NumberPrototype, { name: 'Number', constructor: Number });
642+
wellKnownPrototypes.set(StringPrototype, { name: 'String', constructor: String });
643+
wellKnownPrototypes.set(PromisePrototype, { name: 'Promise', constructor: Promise });
644+
wellKnownPrototypes.set(WeakMapPrototype, { name: 'WeakMap', constructor: WeakMap });
645+
wellKnownPrototypes.set(WeakSetPrototype, { name: 'WeakSet', constructor: WeakSet });
619646

620647
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
621648
let firstProto;
622649
const tmp = obj;
623650
while (obj || isUndetectableObject(obj)) {
624651
const wellKnownPrototypeNameAndConstructor = wellKnownPrototypes.get(obj);
625-
if (wellKnownPrototypeNameAndConstructor != null) {
652+
if (wellKnownPrototypeNameAndConstructor !== undefined) {
626653
const { name, constructor } = wellKnownPrototypeNameAndConstructor;
627654
if (FunctionPrototypeSymbolHasInstance(constructor, tmp)) {
628655
if (protoProps !== undefined && firstProto !== obj) {

0 commit comments

Comments
 (0)