Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Improve arraySeed utility
Browse files Browse the repository at this point in the history
This is a tiny microimprovement, but worthwhile the more we use it.
  • Loading branch information
turt2live committed Jul 12, 2021
1 parent 1b39dbd commit 4910737
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/utils/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ export function arrayRescale(input: number[], newMin: number, newMax: number): n
* @returns {T[]} The array.
*/
export function arraySeed<T>(val: T, length: number): T[] {
const a: T[] = [];
for (let i = 0; i < length; i++) {
a.push(val);
}
return a;
// Size the array up front for performance, and use `fill` to let the browser
// optimize the operation better than we can with a `for` loop, if it wants.
return new Array<T>(length).fill(val);
}

/**
Expand Down

0 comments on commit 4910737

Please sign in to comment.