Skip to content

Commit d52fa71

Browse files
committed
Optimize the sameMap function
1 parent e96ec8c commit d52fa71

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

src/compiler/core.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -435,23 +435,21 @@ namespace ts {
435435
export function sameMap<T>(array: T[], f: (x: T, i: number) => T): T[];
436436
export function sameMap<T>(array: ReadonlyArray<T>, f: (x: T, i: number) => T): ReadonlyArray<T>;
437437
export function sameMap<T>(array: T[], f: (x: T, i: number) => T): T[] {
438-
let result: T[];
439438
if (array) {
440439
for (let i = 0; i < array.length; i++) {
441-
if (result) {
442-
result.push(f(array[i], i));
443-
}
444-
else {
445-
const item = array[i];
446-
const mapped = f(item, i);
447-
if (item !== mapped) {
448-
result = array.slice(0, i);
449-
result.push(mapped);
440+
const item = array[i];
441+
const mapped = f(item, i);
442+
if (item !== mapped) {
443+
const result = array.slice(0, i);
444+
result.push(mapped);
445+
for (i++; i < array.length; i++) {
446+
result.push(f(array[i], i));
450447
}
448+
return result;
451449
}
452450
}
453451
}
454-
return result || array;
452+
return array;
455453
}
456454

457455
/**

0 commit comments

Comments
 (0)