Skip to content

Commit 191533d

Browse files
committed
[Robustness] cache more prototype methods
1 parent 2d2d537 commit 191533d

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

index.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ var weakRefDeref = hasWeakRef ? WeakRef.prototype.deref : null;
1515
var booleanValueOf = Boolean.prototype.valueOf;
1616
var objectToString = Object.prototype.toString;
1717
var functionToString = Function.prototype.toString;
18-
var match = String.prototype.match;
18+
var $match = String.prototype.match;
1919
var $slice = String.prototype.slice;
2020
var $replace = String.prototype.replace;
21-
var test = RegExp.prototype.test;
21+
var $toUpperCase = String.prototype.toUpperCase;
22+
var $toLowerCase = String.prototype.toLowerCase;
23+
var $test = RegExp.prototype.test;
24+
var $concat = Array.prototype.concat;
25+
var $join = Array.prototype.join;
26+
var $arrSlice = Array.prototype.slice;
2227
var $floor = Math.floor;
2328
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
2429
var gOPS = Object.getOwnPropertySymbols;
@@ -44,7 +49,7 @@ function addNumericSeparator(num, str) {
4449
|| num === -Infinity
4550
|| num !== num
4651
|| (num && num > -1000 && num < 1000)
47-
|| test.call(/e/, str)
52+
|| $test.call(/e/, str)
4853
) {
4954
return str;
5055
}
@@ -136,7 +141,7 @@ module.exports = function inspect_(obj, options, depth, seen) {
136141

137142
function inspect(value, from, noIndent) {
138143
if (from) {
139-
seen = seen.slice();
144+
seen = $arrSlice.call(seen);
140145
seen.push(from);
141146
}
142147
if (noIndent) {
@@ -154,21 +159,21 @@ module.exports = function inspect_(obj, options, depth, seen) {
154159
if (typeof obj === 'function') {
155160
var name = nameOf(obj);
156161
var keys = arrObjKeys(obj, inspect);
157-
return '[Function' + (name ? ': ' + name : ' (anonymous)') + ']' + (keys.length > 0 ? ' { ' + keys.join(', ') + ' }' : '');
162+
return '[Function' + (name ? ': ' + name : ' (anonymous)') + ']' + (keys.length > 0 ? ' { ' + $join.call(keys, ', ') + ' }' : '');
158163
}
159164
if (isSymbol(obj)) {
160-
var symString = hasShammedSymbols ? String(obj).replace(/^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj);
165+
var symString = hasShammedSymbols ? $replace.call(String(obj), /^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj);
161166
return typeof obj === 'object' && !hasShammedSymbols ? markBoxed(symString) : symString;
162167
}
163168
if (isElement(obj)) {
164-
var s = '<' + String(obj.nodeName).toLowerCase();
169+
var s = '<' + $toLowerCase.call(String(obj.nodeName));
165170
var attrs = obj.attributes || [];
166171
for (var i = 0; i < attrs.length; i++) {
167172
s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
168173
}
169174
s += '>';
170175
if (obj.childNodes && obj.childNodes.length) { s += '...'; }
171-
s += '</' + String(obj.nodeName).toLowerCase() + '>';
176+
s += '</' + $toLowerCase.call(String(obj.nodeName)) + '>';
172177
return s;
173178
}
174179
if (isArray(obj)) {
@@ -177,12 +182,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
177182
if (indent && !singleLineValues(xs)) {
178183
return '[' + indentedJoin(xs, indent) + ']';
179184
}
180-
return '[ ' + xs.join(', ') + ' ]';
185+
return '[ ' + $join.call(xs, ', ') + ' ]';
181186
}
182187
if (isError(obj)) {
183188
var parts = arrObjKeys(obj, inspect);
184189
if (parts.length === 0) { return '[' + String(obj) + ']'; }
185-
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
190+
return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }';
186191
}
187192
if (typeof obj === 'object' && customInspect) {
188193
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
@@ -230,14 +235,14 @@ module.exports = function inspect_(obj, options, depth, seen) {
230235
var ys = arrObjKeys(obj, inspect);
231236
var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object;
232237
var protoTag = obj instanceof Object ? '' : 'null prototype';
233-
var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? toStr(obj).slice(8, -1) : protoTag ? 'Object' : '';
238+
var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? $slice.call(toStr(obj), 8, -1) : protoTag ? 'Object' : '';
234239
var constructorTag = isPlainObject || typeof obj.constructor !== 'function' ? '' : obj.constructor.name ? obj.constructor.name + ' ' : '';
235-
var tag = constructorTag + (stringTag || protoTag ? '[' + [].concat(stringTag || [], protoTag || []).join(': ') + '] ' : '');
240+
var tag = constructorTag + (stringTag || protoTag ? '[' + $join.call($concat.call([], stringTag || [], protoTag || []), ': ') + '] ' : '');
236241
if (ys.length === 0) { return tag + '{}'; }
237242
if (indent) {
238243
return tag + '{' + indentedJoin(ys, indent) + '}';
239244
}
240-
return tag + '{ ' + ys.join(', ') + ' }';
245+
return tag + '{ ' + $join.call(ys, ', ') + ' }';
241246
}
242247
return String(obj);
243248
};
@@ -248,7 +253,7 @@ function wrapQuotes(s, defaultStyle, opts) {
248253
}
249254

250255
function quote(s) {
251-
return String(s).replace(/"/g, '&quot;');
256+
return $replace.call(String(s), /"/g, '&quot;');
252257
}
253258

254259
function isArray(obj) { return toStr(obj) === '[object Array]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
@@ -299,7 +304,7 @@ function toStr(obj) {
299304

300305
function nameOf(f) {
301306
if (f.name) { return f.name; }
302-
var m = match.call(functionToString.call(f), /^function\s*([\w$]+)/);
307+
var m = $match.call(functionToString.call(f), /^function\s*([\w$]+)/);
303308
if (m) { return m[1]; }
304309
return null;
305310
}
@@ -399,10 +404,10 @@ function inspectString(str, opts) {
399404
if (str.length > opts.maxStringLength) {
400405
var remaining = str.length - opts.maxStringLength;
401406
var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : '');
402-
return inspectString(str.slice(0, opts.maxStringLength), opts) + trailer;
407+
return inspectString($slice.call(str, 0, opts.maxStringLength), opts) + trailer;
403408
}
404409
// eslint-disable-next-line no-control-regex
405-
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
410+
var s = $replace.call($replace.call(str, /(['\\])/g, '\\$1'), /[\x00-\x1f]/g, lowbyte);
406411
return wrapQuotes(s, 'single', opts);
407412
}
408413

@@ -416,7 +421,7 @@ function lowbyte(c) {
416421
13: 'r'
417422
}[n];
418423
if (x) { return '\\' + x; }
419-
return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16).toUpperCase();
424+
return '\\x' + (n < 0x10 ? '0' : '') + $toUpperCase.call(n.toString(16));
420425
}
421426

422427
function markBoxed(str) {
@@ -428,7 +433,7 @@ function weakCollectionOf(type) {
428433
}
429434

430435
function collectionOf(type, size, entries, indent) {
431-
var joinedEntries = indent ? indentedJoin(entries, indent) : entries.join(', ');
436+
var joinedEntries = indent ? indentedJoin(entries, indent) : $join.call(entries, ', ');
432437
return type + ' (' + size + ') {' + joinedEntries + '}';
433438
}
434439

@@ -446,20 +451,20 @@ function getIndent(opts, depth) {
446451
if (opts.indent === '\t') {
447452
baseIndent = '\t';
448453
} else if (typeof opts.indent === 'number' && opts.indent > 0) {
449-
baseIndent = Array(opts.indent + 1).join(' ');
454+
baseIndent = $join.call(Array(opts.indent + 1), ' ');
450455
} else {
451456
return null;
452457
}
453458
return {
454459
base: baseIndent,
455-
prev: Array(depth + 1).join(baseIndent)
460+
prev: $join.call(Array(depth + 1), baseIndent)
456461
};
457462
}
458463

459464
function indentedJoin(xs, indent) {
460465
if (xs.length === 0) { return ''; }
461466
var lineJoiner = '\n' + indent.prev + indent.base;
462-
return lineJoiner + xs.join(',' + lineJoiner) + '\n' + indent.prev;
467+
return lineJoiner + $join.call(xs, ',' + lineJoiner) + '\n' + indent.prev;
463468
}
464469

465470
function arrObjKeys(obj, inspect) {
@@ -486,7 +491,7 @@ function arrObjKeys(obj, inspect) {
486491
if (hasShammedSymbols && symMap['$' + key] instanceof Symbol) {
487492
// this is to prevent shammed Symbols, which are stored as strings, from being included in the string key section
488493
continue; // eslint-disable-line no-restricted-syntax, no-continue
489-
} else if (test.call(/[^\w$]/, key)) {
494+
} else if ($test.call(/[^\w$]/, key)) {
490495
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
491496
} else {
492497
xs.push(key + ': ' + inspect(obj[key], obj));

0 commit comments

Comments
 (0)