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

[Feature]: _fireSelectionEvents easy performance win #8588

Open
4 tasks done
SLKnutson opened this issue Jan 10, 2023 · 2 comments
Open
4 tasks done

[Feature]: _fireSelectionEvents easy performance win #8588

SLKnutson opened this issue Jan 10, 2023 · 2 comments

Comments

@SLKnutson
Copy link
Contributor

CheckList

  • I agree to follow this project's Code of Conduct
  • I have read and followed the Contributing Guide
  • I have searched and referenced existing issues, feature requests and discussions
  • I am filing a FEATURE request.

Description

While profiling, I noticed there is an easy performance win within _fireSelectionEvents. This function shows up in profiling when you are selecting/deselecting a large number of objects (low 1000s). Right now, it determines what is added and what is removed by looping each array and checking if the object exists in the other array. If the existence check was against a set instead of searching the whole list, the performance would be improved, especially when you have large numbers of objects.

I don't have time to do a PR, but I think it would look something like this:

let somethingChanged = false,
  invalidate = false;
const objects = this.getActiveObjects(),
  added: FabricObject[] = [],
  removed: FabricObject[] = [];
const objectsSet = new Set(objects);
const oldObjectsSet = new Set(oldObjects);

oldObjects.forEach((target) => {
  if (!objectsSet.has(target)) {
    somethingChanged = true;
    target.fire('deselected', {
      e,
      target,
    });
    removed.push(target);
  }
});

objects.forEach((target) => {
  if (!oldObjectsSet.has(target)) {
    somethingChanged = true;
    target.fire('selected', {
      e,
      target,
    });
    added.push(target);
  }
});

Current State

Works, but slow for large numbers of objects.
Thanks!

Additional Context

No response

@ShaMan123
Copy link
Contributor

I don't have time to do a PR, but I think it would look something like this:

Hope you find the time, seems good

@proke03
Copy link

proke03 commented Apr 5, 2023

In my opinion, it seems to be an invalid method due to the overhead caused by the operation new Set(arr).

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

No branches or pull requests

3 participants