Skip to content

Commit 3b8092a

Browse files
committed
[Performance] check for primitive types as early as possible.
1 parent aca6265 commit 3b8092a

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,30 @@ var booleanValueOf = Boolean.prototype.valueOf;
1010
var objectToString = Object.prototype.toString;
1111

1212
module.exports = function inspect_ (obj, opts, depth, seen) {
13+
if (typeof obj === 'undefined') {
14+
return 'undefined';
15+
}
16+
if (obj === null) {
17+
return 'null';
18+
}
19+
if (typeof obj === 'boolean') {
20+
return obj ? 'true' : 'false';
21+
}
22+
if (typeof obj === 'string') {
23+
return inspectString(obj);
24+
}
25+
if (typeof obj === 'number') {
26+
if (obj === 0) {
27+
return Infinity / obj > 0 ? '0' : '-0';
28+
}
29+
return String(obj);
30+
}
31+
1332
if (!opts) opts = {};
1433

1534
var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
1635
if (typeof depth === 'undefined') depth = 0;
17-
if (depth >= maxDepth && maxDepth > 0 && obj && typeof obj === 'object') {
36+
if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
1837
return '[Object]';
1938
}
2039

@@ -30,23 +49,11 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
3049
}
3150
return inspect_(value, opts, depth + 1, seen);
3251
}
33-
34-
if (typeof obj === 'string') {
35-
return inspectString(obj);
36-
}
37-
if (typeof obj === 'number') {
38-
if (obj === 0) {
39-
return Infinity / obj > 0 ? '0' : '-0';
40-
}
41-
return String(obj);
42-
}
52+
4353
if (typeof obj === 'function') {
4454
var name = nameOf(obj);
4555
return '[Function' + (name ? ': ' + name : '') + ']';
4656
}
47-
if (obj === null) {
48-
return 'null';
49-
}
5057
if (isSymbol(obj)) {
5158
var symString = Symbol.prototype.toString.call(obj);
5259
return typeof obj === 'object' ? markBoxed(symString) : symString;
@@ -102,9 +109,6 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
102109
});
103110
return collectionOf('Set', setSize.call(obj), parts);
104111
}
105-
if (typeof obj !== 'object') {
106-
return String(obj);
107-
}
108112
if (isNumber(obj)) {
109113
return markBoxed(Number(obj));
110114
}

0 commit comments

Comments
 (0)