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(replace): avoid creating 2 intermediate arrays #61

Merged
merged 3 commits into from
Jul 2, 2024
Merged

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Jul 2, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Generally speaking, a splice should be better performance than allocating 2 arrays that will need to be garbage collected. Not sure how well the benchmarks detect GC-related perf issues, though.

edit: @Minhir realized we could use basic assignment instead of splice. 🎉

Related issue, if any:

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

No

@aleclarson aleclarson added the 🔥 performance An improvement to runtime performance label Jul 2, 2024
for (let index = 0; index < array.length; index++) {
const item = array[index]
if (match(item, index)) {
out.splice(index, 1, newItem)
Copy link
Member

Choose a reason for hiding this comment

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

Why not just out[index] = newItem? out is already a copy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

@aleclarson aleclarson changed the title perf(replace): splice instead of creating 2 intermediate arrays perf(replace): avoid creating 2 intermediate arrays Jul 2, 2024
@aleclarson aleclarson merged commit 20daf2a into main Jul 2, 2024
5 checks passed
@aleclarson aleclarson deleted the perf/replace branch July 2, 2024 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔥 performance An improvement to runtime performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants