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

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

Merged
merged 1 commit into from Jun 9, 2021

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
Contributor

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