mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
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:
committed by
John-David Dalton
parent
e23c874272
commit
b36f21cbaf
@@ -1,15 +1,6 @@
|
|||||||
import baseGetTag from './.internal/baseGetTag.js'
|
import baseGetTag from './.internal/baseGetTag.js'
|
||||||
import isObjectLike from './isObjectLike.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
|
* Checks if `value` is a plain object, that is, an object created by the
|
||||||
* `Object` constructor or one with a `[[Prototype]]` of `null`.
|
* `Object` constructor or one with a `[[Prototype]]` of `null`.
|
||||||
@@ -40,13 +31,14 @@ function isPlainObject(value) {
|
|||||||
if (!isObjectLike(value) || baseGetTag(value) != '[object Object]') {
|
if (!isObjectLike(value) || baseGetTag(value) != '[object Object]') {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
const proto = Object.getPrototypeOf(value)
|
if (Object.getPrototypeOf(value) === null) {
|
||||||
if (proto === null) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor
|
let proto = value
|
||||||
return typeof Ctor == 'function' && Ctor instanceof Ctor &&
|
while (Object.getPrototypeOf(proto) !== null) {
|
||||||
funcToString.call(Ctor) == objectCtorString
|
proto = Object.getPrototypeOf(proto)
|
||||||
|
}
|
||||||
|
return Object.getPrototypeOf(value) === proto
|
||||||
}
|
}
|
||||||
|
|
||||||
export default isPlainObject
|
export default isPlainObject
|
||||||
|
|||||||
Reference in New Issue
Block a user