Faster/simpler isPlainObject check (#3483)

Faster/simpler isPlainObject check.

This runs up the prototype chain to check equivalency. When run in a cross-realm environment (differing contexts, iframes, etc), this ensures we're checking the value's prototype matches its context-specific instance of Object. 

This is faster than calling `toString()` on the constructor. There's still the `baseGetTag()` call, which does its own `toString()`, but this swaps out one for cross-realm stuff. It's also a bit simpler to understand, I think.
This commit is contained in:
Tim Dorr
2017-11-08 14:11:20 -05:00
committed by John-David Dalton
parent e23c874272
commit b36f21cbaf

View File

@@ -1,15 +1,6 @@
import baseGetTag from './.internal/baseGetTag.js'
import isObjectLike from './isObjectLike.js'
/** Used to resolve the decompiled source of functions. */
const funcToString = Function.prototype.toString
/** Used to check objects for own properties. */
const hasOwnProperty = Object.prototype.hasOwnProperty
/** Used to infer the `Object` constructor. */
const objectCtorString = funcToString.call(Object)
/**
* Checks if `value` is a plain object, that is, an object created by the
* `Object` constructor or one with a `[[Prototype]]` of `null`.
@@ -40,13 +31,14 @@ function isPlainObject(value) {
if (!isObjectLike(value) || baseGetTag(value) != '[object Object]') {
return false
}
const proto = Object.getPrototypeOf(value)
if (proto === null) {
if (Object.getPrototypeOf(value) === null) {
return true
}
const Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor
return typeof Ctor == 'function' && Ctor instanceof Ctor &&
funcToString.call(Ctor) == objectCtorString
let proto = value
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
return Object.getPrototypeOf(value) === proto
}
export default isPlainObject