Skip to content

Commit

Permalink
Merge pull request #9 from gamtiq/optimize-replaceAt
Browse files Browse the repository at this point in the history
Make replaceAt faster
  • Loading branch information
guigrpa committed Apr 2, 2017
2 parents 748d828 + f5a6804 commit 0f27e35
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 0f27e35

Please sign in to comment.