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

Performance issue with fixOrder #15

Open
vijayst opened this issue Apr 25, 2017 · 1 comment
Open

Performance issue with fixOrder #15

vijayst opened this issue Apr 25, 2017 · 1 comment

Comments

@vijayst
Copy link
Contributor

vijayst commented Apr 25, 2017

Using fixOrder on a list of 3000 items took 30 seconds. Knowing the nature of data (only parent-child relation), an alternative fix took only 50 ms.

Random list of 3000 items:

     const rows = [];
     let j = 0;
     for (let i = 0; i < 3000; i++) {
         if (i % 3 === 0) {
             j++;
             rows.push({
                 id: j,
                 name: `Item ${j}`,
             });
        } else {
            const parent = Math.round(Math.random() * 1000);
            rows.push({
                id: 10000 + i,
                name: `Item ${10000 + i}`,
                parent
            });
        }
     }

An alternative fixOrder which works in 50 ms (not generic):

function fixOrder2(rows) {
    const parents = rows.filter(r => !r.parent);
    const children = rows.filter(r => r.parent);
    children.forEach(child => {
        const parentIndex = parents.findIndex(p => p.id === child.parent);
        parents.splice(parentIndex + 1, 0, child);
    });
    return parents;
}
@bebraw
Copy link
Member

bebraw commented Apr 26, 2017

Good catch. I wonder what would be a good way to handle this. Could the default algorithm be made faster? If not, then adding another function might be a nice way. I can accept a PR for either.

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

No branches or pull requests

2 participants