Skip to content

Commit f901443

Browse files
Brandon Benviekoichik
Brandon Benvie
authored andcommitted
util: use getOwnPropertyDescripter
Change formatProperty in util.js to use Object.getOwnPropertyDescriptor instead of __lookup[GS]etter__. Use the cached value from the descriptor to reduce number of property lookups from 3 to 1. Fallback to standard lookup if the descriptor is empty. This doesn't ever happen with normal JS objects (this function is called only when the key exists) but apparently does with Node's custom ENV interface. Fixes: #2109.
1 parent 3df7c90 commit f901443

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

lib/util.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,29 +296,28 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
296296

297297

298298
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
299-
var name, str;
300-
if (value.__lookupGetter__) {
301-
if (value.__lookupGetter__(key)) {
302-
if (value.__lookupSetter__(key)) {
303-
str = ctx.stylize('[Getter/Setter]', 'special');
304-
} else {
305-
str = ctx.stylize('[Getter]', 'special');
306-
}
299+
var name, str, desc;
300+
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
301+
if (desc.get) {
302+
if (desc.set) {
303+
str = ctx.stylize('[Getter/Setter]', 'special');
307304
} else {
308-
if (value.__lookupSetter__(key)) {
309-
str = ctx.stylize('[Setter]', 'special');
310-
}
305+
str = ctx.stylize('[Getter]', 'special');
306+
}
307+
} else {
308+
if (desc.set) {
309+
str = ctx.stylize('[Setter]', 'special');
311310
}
312311
}
313312
if (visibleKeys.indexOf(key) < 0) {
314313
name = '[' + key + ']';
315314
}
316315
if (!str) {
317-
if (ctx.seen.indexOf(value[key]) < 0) {
316+
if (ctx.seen.indexOf(desc.value) < 0) {
318317
if (recurseTimes === null) {
319-
str = formatValue(ctx, value[key], null);
318+
str = formatValue(ctx, desc.value, null);
320319
} else {
321-
str = formatValue(ctx, value[key], recurseTimes - 1);
320+
str = formatValue(ctx, desc.value, recurseTimes - 1);
322321
}
323322
if (str.indexOf('\n') > -1) {
324323
if (array) {

0 commit comments

Comments
 (0)