From b36f21cbaff7c885223b44267c70e6711b754af6 Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Wed, 8 Nov 2017 14:11:20 -0500 Subject: [PATCH] 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. --- isPlainObject.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/isPlainObject.js b/isPlainObject.js index beba9ec21..34dcf9af8 100644 --- a/isPlainObject.js +++ b/isPlainObject.js @@ -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