Skip to content

Commit

Permalink
Merge pull request #8620 from ckeditor/i/8188
Browse files Browse the repository at this point in the history
Other (engine): Improved performance of `Differ#getChanges()`. Closes #8188.
  • Loading branch information
scofalik committed Dec 10, 2020
2 parents 641bfe8 + c8cdacf commit 98644f6
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/ckeditor5-engine/src/model/differ.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export default class Differ {
}

// Will contain returned results.
const diffSet = [];
let diffSet = [];

// Check all changed elements.
for ( const element of this._changesInElement.keys() ) {
Expand Down Expand Up @@ -483,8 +483,8 @@ export default class Differ {
} );

// Glue together multiple changes (mostly on text nodes).
for ( let i = 1; i < diffSet.length; i++ ) {
const prevDiff = diffSet[ i - 1 ];
for ( let i = 1, prevIndex = 0; i < diffSet.length; i++ ) {
const prevDiff = diffSet[ prevIndex ];
const thisDiff = diffSet[ i ];

// Glue remove changes if they happen on text on same position.
Expand All @@ -511,17 +511,20 @@ export default class Differ {
prevDiff.attributeNewValue == thisDiff.attributeNewValue;

if ( isConsecutiveTextRemove || isConsecutiveTextAdd || isConsecutiveAttributeChange ) {
diffSet[ i - 1 ].length++;
prevDiff.length++;

if ( isConsecutiveAttributeChange ) {
diffSet[ i - 1 ].range.end = diffSet[ i - 1 ].range.end.getShiftedBy( 1 );
prevDiff.range.end = prevDiff.range.end.getShiftedBy( 1 );
}

diffSet.splice( i, 1 );
i--;
diffSet[ i ] = null;
} else {
prevIndex = i;
}
}

diffSet = diffSet.filter( v => v );

// Remove `changeCount` property from diff items. It is used only for sorting and is internal thing.
for ( const item of diffSet ) {
delete item.changeCount;
Expand All @@ -536,7 +539,7 @@ export default class Differ {

// Cache changes.
this._cachedChangesWithGraveyard = diffSet.slice();
this._cachedChanges = diffSet.slice().filter( _changesInGraveyardFilter );
this._cachedChanges = diffSet.filter( _changesInGraveyardFilter );

if ( options.includeChangesInGraveyard ) {
return this._cachedChangesWithGraveyard;
Expand Down

0 comments on commit 98644f6

Please sign in to comment.