Skip to content

Commit e27b788

Browse files
Avoid unnecesary prototype loop
As the whole purpose of the `while (Object.getPrototypeOf(proto) !== null)` loop seems to counter validate there was nothing else to loop, I believe it's a performance benefit, as well as smaller code, to simply check the prototype never more than 2 levels, with a graceful fallback in case the first check resulted into `null` already: * the amount of `Object.getPrototypeOf` is fixed as `2` and never more * there is less code to shrink/optimize for bundlers or minifiers
1 parent 2712541 commit e27b788

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/isPlainObject.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
* @returns {boolean} True if the argument appears to be a plain object.
88
*/
99
export default function isPlainObject(value: unknown): value is object {
10-
if (typeof value !== 'object' || value === null) return false
11-
12-
let proto = value
13-
while (Object.getPrototypeOf(proto) !== null) {
14-
proto = Object.getPrototypeOf(proto)
15-
}
16-
17-
return Object.getPrototypeOf(value) === proto
10+
return (
11+
typeof value === 'object' &&
12+
value !== null &&
13+
Object.getPrototypeOf(Object.getPrototypeOf(value) || 0) === null
14+
)
1815
}

0 commit comments

Comments
 (0)