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: array optimizations #2852

Draft
wants to merge 7 commits into
base: v3
Choose a base branch
from

Conversation

GalacticHypernova
Copy link

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

While a less-known optimization, pre-allocating arrays allows the JavaScript engine to prepare all the memory upfront for the array and prevent dynamic resizing. Directly accessing arrays would be O(1) as opposed to Array.prototype.push being amortized O(1) due to the occasional dynamic resizing which is O(n).

This PR, while having the pre-allocated arrays as the core change, simplifies affected areas by, for example, reducing iteration and mapping counts for arrays whose size can be determined immediately, and thus can be pre-allocated. By doing that, in areas with multiple arrays (such as the CSS plugin that registers the initial root variables), we can use a single loop and consolidate the mapping logic directly in it.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@GalacticHypernova
Copy link
Author

Marking as draft so I could take my time and make sure I get everything I can

@benjamincanac
Copy link
Member

What are you trying to solve here? πŸ€”

@benjamincanac benjamincanac added the v3 #1289 label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 #1289
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants