-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
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
Labels
No labels