@@ -129,17 +129,30 @@ class Trait<T> implements ISpliceable<boolean>, IDisposable {
129
129
130
130
const diff = elements . length - deleteCount ;
131
131
const end = start + deleteCount ;
132
- const sortedIndexes = [
133
- ...this . sortedIndexes . filter ( i => i < start ) ,
134
- ...elements . map ( ( hasTrait , i ) => hasTrait ? i + start : - 1 ) . filter ( i => i !== - 1 ) ,
135
- ...this . sortedIndexes . filter ( i => i >= end ) . map ( i => i + diff )
136
- ] ;
132
+ const sortedIndexes : number [ ] = [ ] ;
133
+ let firstSortedIndex : number | undefined = undefined ;
134
+ let i = 0 ;
135
+
136
+ while ( i < this . sortedIndexes . length && this . sortedIndexes [ i ] < start ) {
137
+ sortedIndexes . push ( this . sortedIndexes [ i ++ ] ) ;
138
+ }
139
+
140
+ for ( let j = 0 ; j < elements . length ; j ++ ) {
141
+ if ( elements [ j ] ) {
142
+ sortedIndexes . push ( j + start ) ;
143
+ firstSortedIndex = firstSortedIndex ?? sortedIndexes [ sortedIndexes . length - 1 ] ;
144
+ }
145
+ }
146
+
147
+ while ( i < this . sortedIndexes . length && this . sortedIndexes [ i ] >= end ) {
148
+ sortedIndexes . push ( this . sortedIndexes [ i ++ ] + diff ) ;
149
+ firstSortedIndex = firstSortedIndex ?? sortedIndexes [ sortedIndexes . length - 1 ] ;
150
+ }
137
151
138
152
const length = this . length + diff ;
139
153
140
154
if ( this . sortedIndexes . length > 0 && sortedIndexes . length === 0 && length > 0 ) {
141
- const first = this . sortedIndexes . find ( index => index >= start ) ?? length - 1 ;
142
- sortedIndexes . push ( Math . min ( first , length - 1 ) ) ;
155
+ sortedIndexes . push ( Math . min ( firstSortedIndex ?? length - 1 , length - 1 ) ) ;
143
156
}
144
157
145
158
this . renderer . splice ( start , deleteCount , elements . length ) ;
@@ -226,12 +239,16 @@ class TraitSpliceable<T> implements ISpliceable<T> {
226
239
227
240
splice ( start : number , deleteCount : number , elements : T [ ] ) : void {
228
241
if ( ! this . identityProvider ) {
229
- return this . trait . splice ( start , deleteCount , elements . map ( ( ) => false ) ) ;
242
+ return this . trait . splice ( start , deleteCount , new Array ( elements . length ) . fill ( false ) ) ;
230
243
}
231
244
232
245
const pastElementsWithTrait = this . trait . get ( ) . map ( i => this . identityProvider ! . getId ( this . view . element ( i ) ) . toString ( ) ) ;
233
- const elementsWithTrait = elements . map ( e => pastElementsWithTrait . indexOf ( this . identityProvider ! . getId ( e ) . toString ( ) ) > - 1 ) ;
246
+ if ( pastElementsWithTrait . length === 0 ) {
247
+ return this . trait . splice ( start , deleteCount , new Array ( elements . length ) . fill ( false ) ) ;
248
+ }
234
249
250
+ const pastElementsWithTraitSet = new Set ( pastElementsWithTrait ) ;
251
+ const elementsWithTrait = elements . map ( e => pastElementsWithTraitSet . has ( this . identityProvider ! . getId ( e ) . toString ( ) ) ) ;
235
252
this . trait . splice ( start , deleteCount , elementsWithTrait ) ;
236
253
}
237
254
}
0 commit comments