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

refactor: Optimize Array Creation for Improved Performance #31399

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dutexion
Copy link

@dutexion dutexion commented Nov 1, 2024

Before diving into the details......
I'm still quite new to development and I don't have much experience contributing to open source projects yet, so I realize that there might be areas where my work is lacking. If there are any issues or if you have any advice on how I could improve, I would greatly appreciate your feedback.

Summary

This Pull Request optimizes the array creation method to improve overall performance. Specifically, the change involves switching from Array.from({ length: size }) to Array.from(Array(size)), which has shown to reduce overhead during array creation.

Motivation

During performance analysis, it was found that using Array.from({ length: size }) has slightly higher overhead compared to Array.from(Array(size)). Given that array creation is a fundamental operation that may be repeated many times in various use cases, even a small improvement in performance can lead to noticeable gains, particularly for large-scale or frequent operations.

Changes Made

  • Modified array creation logic: Changed from Array.from({ length: size }) to Array.from(Array(size)).
  • The new approach is more efficient and reduces the time needed to create large arrays.

Performance Testing

To verify the performance gains, the following test was conducted:

  1. Created two versions of the generateArray function, one using Array.from(Array(size)) and the other using Array.from({ length: size }).
  2. Executed both versions across 1000 iterations with an array size of 50,000.
  3. Calculated the average time taken for each approach.

Results

  • Array.from(Array(size)): 1382.79 ms (average across 1000 iterations)
  • Array.from({ length: size }): 1736.04 ms (average across 1000 iterations)
    The data clearly indicates that the new approach (Array.from(Array(size))) provides a significant performance improvement, reducing the average time by approximately 20%.

Code Example

Below are the test functions used to benchmark the performance:

Old Implementation:

function generateArray(size) {
  return Array.from({ length: size }, () => Math.floor(Math.random() * size));
}

New Implementation:

function generateArray(size) {
  return Array.from(Array(size), () => Math.floor(Math.random() * size));
}

Testing

  • Benchmarked both versions using the console.time() method to measure and compare execution time.
  • Verified that the new implementation consistently outperformed the old one in multiple runs.
  • Ensured all existing unit tests pass to validate the correctness of the change.

Conclusion

By modifying the array creation method, this PR aims to improve the efficiency of operations that involve large array instantiations. This change should be especially beneficial for performance-critical applications where array creation is a bottleneck.

Copy link

vercel bot commented Nov 1, 2024

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

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 1, 2024 2:36pm

@facebook-github-bot
Copy link

Hi @dutexion!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants