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(RoleManager): dont call Role#position getter twice per role #9352

Merged
merged 2 commits into from Apr 9, 2023

Conversation

almeidx
Copy link
Member

@almeidx almeidx commented Apr 9, 2023

Please describe the changes this PR makes and why it should be merged:
Stored the position of the roles to not call the position getter twice per role as it is a reasonably heavy operation

Role#position

get position() {
const sorted = this.guild._sortedRoles();
return [...sorted.values()].indexOf(sorted.get(this.id));
}

_sortedRoles() {
return discordSort(this.roles.cache);
}

function discordSort(collection) {
const isGuildChannel = collection.first() instanceof GuildChannel;
return collection.sorted(
isGuildChannel
? (a, b) => a.rawPosition - b.rawPosition || Number(BigInt(a.id) - BigInt(b.id))
: (a, b) => a.rawPosition - b.rawPosition || Number(BigInt(b.id) - BigInt(a.id)),
);
}

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

@vercel
Copy link

vercel bot commented Apr 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
discord-js-guide ⬜️ Ignored (Inspect) Apr 9, 2023 10:26am

@vercel
Copy link

vercel bot commented Apr 9, 2023

@almeidx is attempting to deploy a commit to the discordjs Team on Vercel.

A member of the Team first needs to authorize it.

@kodiakhq kodiakhq bot merged commit bfee6c8 into discordjs:main Apr 9, 2023
5 of 6 checks passed
@almeidx almeidx deleted the discord.js/compare-positions-cache branch April 9, 2023 10:29
jaw0r3k added a commit to jaw0r3k/discord.js that referenced this pull request Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

5 participants