Skip to content

perf: isPlainObject: add quick comparison between input and Object to short-circuit taxing Function.toString invocations#805

Merged
mweststrate merged 1 commit intoimmerjs:masterfrom
benelliott:fix/is-plain-object
Jun 9, 2021
Merged

perf: isPlainObject: add quick comparison between input and Object to short-circuit taxing Function.toString invocations#805
mweststrate merged 1 commit intoimmerjs:masterfrom
benelliott:fix/is-plain-object

Conversation

@benelliott
Copy link
Contributor

Fixes #804

…to short-circuit taxing `Function.toString` invocations
@mweststrate mweststrate merged commit 07575f3 into immerjs:master Jun 9, 2021
@mweststrate
Copy link
Collaborator

Thanks for optimising this!

@mweststrate
Copy link
Collaborator

🎉 This PR is included in version 9.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markerikson
Copy link
Collaborator

FWIW, here's the implementation that Redux uses:

https://github.com/reduxjs/redux/blob/master/src/utils/isPlainObject.ts

/**
 * @param obj The object to inspect.
 * @returns True if the argument appears to be a plain object.
 */
export default function isPlainObject(obj: any): boolean {
  if (typeof obj !== 'object' || obj === null) return false

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

  return Object.getPrototypeOf(obj) === proto
}

I believe this also correctly handles cross-frame behavior. If I remember right, @timdorr implemented this. Backstory:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance regression in isPlainObject utility

3 participants