Skip to content

Commit

Permalink
Improve time complexity for getGarbageLen
Browse files Browse the repository at this point in the history
  • Loading branch information
JongBeom Lee committed Jan 3, 2024
1 parent 79f0138 commit 610516f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/document/crdt/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,19 @@ export class CRDTRoot {
public getGarbageLen(): number {
let count = 0;
const seen = new Set<string>();
const sortedRemovedElementSetByCreatedAt = Array.from(
this.removedElementSetByCreatedAt,
).sort();

for (const createdAt of this.removedElementSetByCreatedAt) {
for (const createdAt of sortedRemovedElementSetByCreatedAt) {
seen.add(createdAt);
const pair = this.elementPairMapByCreatedAt.get(createdAt)!;
if (pair.element instanceof CRDTContainer) {
pair.element.getDescendants((el) => {
const idString = el.getCreatedAt().toIDString();
if (seen.has(idString)) {
return true;
}
seen.add(el.getCreatedAt().toIDString());
return false;
});
Expand Down

0 comments on commit 610516f

Please sign in to comment.