Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecesary prototype loop #581

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/isPlainObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
* @returns {boolean} True if the argument appears to be a plain object.
*/
export default function isPlainObject(value: unknown): value is object {
if (typeof value !== 'object' || value === null) return false

let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}

return Object.getPrototypeOf(value) === proto
return (
typeof value === 'object' &&
value !== null &&
Object.getPrototypeOf(Object.getPrototypeOf(value) || 0) === null
Copy link
Author

@WebReflection WebReflection May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. the 0 could be just a literal {} instead, it'd work exactly the same (but it'd be 1 extra char).

)
}