@@ -33,7 +33,14 @@ export function proxifyArrayElements<T extends any[]>(
3333 rest [ 0 ] as number ,
3434 ...rest . slice ( 1 ) . map ( n => literalToAst ( n ) ) ,
3535 ) ;
36- return deleted . map ( n => proxify ( n as any , mod ) ) ;
36+ const result : any [ ] = [ ] ;
37+ result . length = deleted . length ;
38+ for ( let index = 0 ; index < deleted . length ; index ++ ) {
39+ if ( deleted [ index ] != null ) {
40+ result [ index ] = proxify ( deleted [ index ] , mod ) ;
41+ }
42+ }
43+ return result ;
3744 } ,
3845 toJSON ( ) {
3946 return elements . map ( n => proxify ( n as any , mod ) ) ;
@@ -50,11 +57,13 @@ export function proxifyArrayElements<T extends any[]>(
5057 const self = receiver as any [ ] ;
5158 if ( key === "map" ) {
5259 return ( callback : ( value : any , index : number , array : any [ ] ) => any ) => {
53- const results = [ ] ;
54- let index = 0 ;
55- for ( const item of self ) {
56- results . push ( callback ( item , index , self ) ) ;
57- index ++ ;
60+ const results : any [ ] = [ ] ;
61+ const length = elements . length ;
62+ results . length = length ;
63+ for ( let index = 0 ; index < length ; index ++ ) {
64+ if ( elements [ index ] != null ) {
65+ results [ index ] = callback ( proxify ( elements [ index ] , mod ) , index , self ) ;
66+ }
5867 }
5968 return results ;
6069 } ;
@@ -64,12 +73,15 @@ export function proxifyArrayElements<T extends any[]>(
6473 callback : ( value : any , index : number , array : any [ ] ) => boolean ,
6574 ) => {
6675 const results = [ ] ;
67- let index = 0 ;
68- for ( const item of self ) {
76+ const length = elements . length ;
77+ for ( let index = 0 ; index < length ; index ++ ) {
78+ if ( elements [ index ] == null ) {
79+ continue ;
80+ }
81+ const item = proxify ( elements [ index ] , mod ) ;
6982 if ( callback ( item , index , self ) ) {
7083 results . push ( item ) ;
7184 }
72- index ++ ;
7385 }
7486 return results ;
7587 } ;
@@ -78,10 +90,11 @@ export function proxifyArrayElements<T extends any[]>(
7890 return (
7991 callback : ( value : any , index : number , array : any [ ] ) => void ,
8092 ) => {
81- let index = 0 ;
82- for ( const item of self ) {
83- callback ( item , index , self ) ;
84- index ++ ;
93+ const length = elements . length ;
94+ for ( let index = 0 ; index < length ; index ++ ) {
95+ if ( elements [ index ] != null ) {
96+ callback ( proxify ( elements [ index ] , mod ) , index , self ) ;
97+ }
8598 }
8699 } ;
87100 }
@@ -95,24 +108,32 @@ export function proxifyArrayElements<T extends any[]>(
95108 ) => any ,
96109 ...initialValue : [ any ?]
97110 ) => {
98- const array = [ ...self ] ;
99- if ( array . length === 0 && initialValue . length === 0 ) {
100- throw new TypeError ( "Reduce of empty array with no initial value" ) ;
101- }
102-
103111 let accumulator : any ;
104112 let startIndex = 0 ;
113+ const length = elements . length ;
105114
106115 if ( initialValue . length > 0 ) {
107116 accumulator = initialValue [ 0 ] ;
108117 }
109118 else {
110- accumulator = array [ 0 ] ;
111- startIndex = 1 ;
119+ while ( startIndex < length && elements [ startIndex ] == null ) {
120+ startIndex ++ ;
121+ }
122+ if ( startIndex === length ) {
123+ throw new TypeError ( "Reduce of empty array with no initial value" ) ;
124+ }
125+ accumulator = proxify ( elements [ startIndex ++ ] , mod ) ;
112126 }
113127
114- for ( let i = startIndex ; i < array . length ; i ++ ) {
115- accumulator = callback ( accumulator , array [ i ] , i , array ) ;
128+ for ( let index = startIndex ; index < length ; index ++ ) {
129+ if ( elements [ index ] != null ) {
130+ accumulator = callback (
131+ accumulator ,
132+ proxify ( elements [ index ] , mod ) ,
133+ index ,
134+ self ,
135+ ) ;
136+ }
116137 }
117138
118139 return accumulator ;
@@ -222,7 +243,7 @@ export function proxifyArrayElements<T extends any[]>(
222243 }
223244
224245 const index = + key ;
225- if ( ! Number . isNaN ( index ) && index < elements . length ) {
246+ if ( ! Number . isNaN ( index ) && index < elements . length && elements [ index ] != null ) {
226247 return {
227248 value : proxify ( elements [ index ] , mod ) ,
228249 writable : true ,
0 commit comments