Skip to content

Commit

Permalink
Make replaceAt faster
Browse files Browse the repository at this point in the history
  • Loading branch information
gamtiq committed Mar 23, 2017
1 parent 748d828 commit f5a6804
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/timm.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,14 @@ export function removeAt<T>(array: Array<T>, idx: number): Array<T> {
// -- ```
export function replaceAt<T>(array: Array<T>, idx: number, newItem: T): Array<T> {
if (array[idx] === newItem) return array;
return array
.slice(0, idx)
.concat([newItem])
.concat(array.slice(idx + 1));

const len: number = array.length;
const result: Array<T> = Array(len);
for (let i = 0; i < len; i++) {
result[i] = array[i];
}
result[idx] = newItem;
return result;
}

// ===============================================
Expand Down

0 comments on commit f5a6804

Please sign in to comment.