Skip to content

ref(js): spring cleaning - #48116

Closed
JonasBa wants to merge 4 commits into
masterfrom
jb/feat/spring-cleaning-vol1
Closed

ref(js): spring cleaning#48116
JonasBa wants to merge 4 commits into
masterfrom
jb/feat/spring-cleaning-vol1

Conversation

@JonasBa

@JonasBa JonasBa commented Apr 27, 2023

Copy link
Copy Markdown
Contributor

I have been experimenting with some eslint rules to detect performance issues as per getsentry/sentry-javascript#7910 and #47629. All of this is pretty much in the same spirit where we try to find On^2 code + unnecessary array rest/spread.

Figured it wouldn't hurt to clean some of the code in Sentry anyways so I'm opening this as draft and awareness. I would not expect any of these to be actual performance issues, but the code is still wasteful and in some cases just less readable (like the ...set.length vs just checking set.size)

Lmk what you think, I'd be curious to hear some thoughts and opinions, the only thing to cleanup is just the chang to eslintrc which was linked for testing purposes

@JonasBa
JonasBa requested review from a team, evanpurkhiser, malwilley and scttcper April 27, 2023 22:09
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Apr 27, 2023
Comment on lines +62 to +70
const selectedIssues: BaseGroup[] = [];
for (const issue of issues) {
const resolvedIssue = GroupStore.get(issue);

if (resolvedIssue) {
// @TODO use a type guard here
selectedIssues.push(resolvedIssue as BaseGroup);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels harder to read

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could just switch it to issues.map so there isn't an extra array being made

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, set.forEach is better here, same with below comment I think

Comment on lines +276 to +284
const projects: string[] = [];

for (const selectedGroup of selected) {
const group = GroupStore.get(selectedGroup);

if (group && group.project) {
projects.push(group.project.slug);
}
}

@scttcper scttcper Apr 27, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like the map and filter should just be written better

maybe

const projects = selected
    .map(id => GroupStore.get(id)?.project?.slug)
    .filter(defined)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work because selected is a Set :/

Comment on lines +237 to +250
newProject = memberProjects?.[0]
? [getProjectIdFromProject(memberProjects[0])]
: [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined is not iterable, thus if memberProjects is undefined,[...memberProjects] would already fail earlier anyway, which mean we can write :P

Suggested change
newProject = memberProjects?.[0]
? [getProjectIdFromProject(memberProjects[0])]
: [];
newProject = [getProjectIdFromProject(memberProjects[0])]

@JonasBa JonasBa Apr 28, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what happens is that calling memberProjects with undefined throws (least from looking at the test failures), so I'd need to protect from that. I wrote that exact line at first hehe
CleanShot 2023-04-28 at 13 20 34@2x

Comment on lines +128 to +131
const emptyValue = mappedKeys.reduce((a, v) => {
a[v] = null;
return a;
}, {});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

Suggested change
const emptyValue = mappedKeys.reduce((a, v) => {
a[v] = null;
return a;
}, {});
const emptyValue = Object.fromEntries(mappedKeys.map(key => ([key, null])));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, I like that. I think we had some errors around Object.fromEntries in unsupported browsers though, I'd need to check


const mappedKeys = columnKeys || [];
const emptyValue = mappedKeys.reduce((a, v) => ({...a, [v]: null}), {id: ''});
const emptyValue = mappedKeys.reduce(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, but with .concat(['id', '']) at the end of map.

Comment on lines +34 to +36
list.reduce<SDKUpdatesSuggestion[]>((suggestions, sdk) => {
return suggestions.concat(sdk.suggestions);
}, []);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
list.reduce<SDKUpdatesSuggestion[]>((suggestions, sdk) => {
return suggestions.concat(sdk.suggestions);
}, []);
list.flatMap(l => l.suggestions) as SDKUpdatesSuggestion[];

(not sure if type assertion is required)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, much nicer. I keep forgetting about flatMap

@JonasBa
JonasBa force-pushed the jb/feat/spring-cleaning-vol1 branch from 0ab4326 to a296008 Compare May 17, 2023 14:14
@github-actions

github-actions Bot commented Jun 8, 2023

Copy link
Copy Markdown
Contributor

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@github-actions github-actions Bot closed this Jun 17, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 2, 2023
@asottile-sentry
asottile-sentry deleted the jb/feat/spring-cleaning-vol1 branch December 27, 2023 16:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants