Skip to content

Performance issue with fixOrder #15

@vijayst

Description

@vijayst

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions