Skip to content

Commit cae2d8d

Browse files
author
Alec Gibson
committed
Improve performance of getLinkedOps
Running `getOps` to fetch a large number of operations is not very performant. Anecdotally, performing this operation on a local development machine with a document comprising of ~200,000 operations takes ~20s. The largest slow-down appears to come from the `getLinkedOps` method, and in particular the use of `Array.unshift`. This change "inverts" the logic of the `getLinkedOps` function. Rather than building up an array from scratch, instead we create a shallow copy of the `ops` array using `slice`, and remove any operations we don't want using `splice`. The anecdotal operation time using this modified function is now ~2s, which is an improvement of an order of magnitude. The performance when fetching a small number of operations is similar.
1 parent 5a7c674 commit cae2d8d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,21 @@ function getLatestDeleteOp(ops) {
526526
}
527527

528528
function getLinkedOps(ops, to, link) {
529-
var linkedOps = []
529+
var linkedOps = ops.slice(0);
530+
530531
for (var i = ops.length; i-- && link;) {
531532
var op = ops[i];
532-
if (link.equals ? !link.equals(op._id) : link !== op._id) continue;
533+
534+
if (link.equals ? !link.equals(op._id) : link !== op._id) {
535+
linkedOps.splice(i, 1);
536+
}
537+
533538
link = op.o;
534539
if (to == null || op.v < to) {
535540
delete op._id;
536541
delete op.o;
537-
linkedOps.unshift(op);
542+
} else {
543+
linkedOps.splice(i, 1);
538544
}
539545
}
540546
return linkedOps;

0 commit comments

Comments
 (0)