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

Replace for/push loop with Array of initial size #934

Merged
merged 2 commits into from
Apr 15, 2022

Conversation

edemaine
Copy link
Contributor

@edemaine edemaine commented Apr 13, 2022

I noticed this bit of code which builds an array via push incrementally instead of just calling map. I believe map is faster because it can pre-allocate an array of the right size. This jsbench confirms my hypothesis. The code is also shorter and cleaner.

Or is map being avoided on purpose for compatibility? It's ECMAScript 5. MDN; Can I use.

[discussion on Discord]

@coveralls
Copy link

coveralls commented Apr 13, 2022

Pull Request Test Coverage Report for Build 2164463861

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.004%) to 89.563%

Totals Coverage Status
Change from base Build 2147526457: 0.004%
Covered Lines: 1217
Relevant Lines: 1296

💛 - Coveralls

@ryansolid
Copy link
Member

ryansolid commented Apr 13, 2022

Hmm.. I tested this a lot before. For wasn't only faster it was like 30% faster in my benchmarks. When I was struggling to get the fastest SSR this one of the blockers that led me to creating a separate runtime for server. Then I looked at what Marko was doing and using for + push was the key.

I'm sort of surprised map is faster. It never has been historically, but it's worth reviewing.

Also Chrome 100 is a bit crazy right now. It sort of broke a bunch of the benchmarks. I think we need to test in a V8 node server environment.

@edemaine edemaine changed the title Replace for/push loop with map Replace for/push loop with Array of initial size Apr 14, 2022
@edemaine
Copy link
Contributor Author

liquid on Discord pointed out that

  • The map version is slower for large arrays (for me, the crossover point is around 20 items), and
  • There's a for-loop version that is the best of both worlds: preallocate an Array of the correct size. Here's the new benchmark. It's faster for arrays of size 1, 4, 20, etc.

I pushed code for the new version.

@ryansolid ryansolid merged commit 4baa3db into solidjs:main Apr 15, 2022
@ryansolid
Copy link
Member

Thank you.

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

Successfully merging this pull request may close these issues.

3 participants