File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -147,17 +147,29 @@ namespace ts {
147
147
return count ;
148
148
}
149
149
150
+ /**
151
+ * Filters an array by a predicate function. Returns the same array instance if the predicate is
152
+ * true for all elements, otherwise returns a new array instance containing the filtered subset.
153
+ */
150
154
export function filter < T > ( array : T [ ] , f : ( x : T ) => boolean ) : T [ ] {
151
- let result : T [ ] ;
152
155
if ( array ) {
153
- result = [ ] ;
154
- for ( const item of array ) {
155
- if ( f ( item ) ) {
156
- result . push ( item ) ;
156
+ const len = array . length ;
157
+ let i = 0 ;
158
+ while ( i < len && f ( array [ i ] ) ) i ++ ;
159
+ if ( i < len ) {
160
+ const result = array . slice ( 0 , i ) ;
161
+ i ++ ;
162
+ while ( i < len ) {
163
+ const item = array [ i ] ;
164
+ if ( f ( item ) ) {
165
+ result . push ( item ) ;
166
+ }
167
+ i ++ ;
157
168
}
169
+ return result ;
158
170
}
159
171
}
160
- return result ;
172
+ return array ;
161
173
}
162
174
163
175
export function filterMutate < T > ( array : T [ ] , f : ( x : T ) => boolean ) : void {
You can’t perform that action at this time.
0 commit comments