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

Trivial performance bug in ObservableMap.replace #2003

Closed
3 tasks done
Maaartinus opened this issue Jun 13, 2019 · 6 comments
Closed
3 tasks done

Trivial performance bug in ObservableMap.replace #2003

Maaartinus opened this issue Jun 13, 2019 · 6 comments

Comments

@Maaartinus
Copy link

Maaartinus commented Jun 13, 2019

I have a:

  1. Issue:
  • Did you check this issue wasn't filed before?

No behavior change; it's just performance bug.

  • State the versions of MobX and relevant libraries. Which browser / node / ... version?

const oldKeys = Array.from(this.keys())

I'm proposing the following trivial change:

  •        const newKeys = (getMapLikeKeys(values) as any) as K[]
          const oldKeys = Array.from(this.keys())
    
  •        const missingKeys = oldKeys.filter(k => newKeys.indexOf(k) === -1)
    
  •       const missingKeys = oldKeys.filter(k => !values.has(k))
    

The current behavior has quadratic complexity, with two nested loops (over all old and new keys). So for a map with a few thousands elements it may take ages, while values.has is supposed to be a constant-time operation (leading to linear overall complexity).

@urugator
Copy link
Collaborator

Note that values is not neccessarily a Map, it can be object { key: value } or array [[key,value]], so you may need to convert it first:

if (isPlainObject(values)) {
  // could be done in single pass, but whatever
  values = Object.entries(values);
}
if (Array.isArray(values)) {
  values = new Map(values);
} 

However I think we should resolve #1980 first.

@stale
Copy link

stale bot commented Jun 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale
Copy link

stale bot commented Jun 28, 2019

This issue has been automatically unmarked as stale. Please disregard previous warnings.

@danielkcz
Copy link
Contributor

PR #2057 merged and waiting for the release

@Bnaya
Copy link
Member

Bnaya commented Dec 15, 2019

published

@lock
Copy link

lock bot commented Feb 13, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants