Skip to content

Commit 0607056

Browse files
committed
perf: move mapping of initialValues to final recursive function call
The mapping of intialValues into the matrix now will only occur in the final recursive call of makeMatrix instead of at all calls. This should help with performance, especially when a callback is used to intialise values across ther matrix.
1 parent 4097bb8 commit 0607056

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ function makeMatrix<T>(
8585

8686
// Spread operator used as as constructed array's can't be mapped:
8787
// https://itnext.io/heres-why-mapping-a-constructed-array-doesn-t-work-in-javascript-f1195138615a
88-
const currentMatrix = [...Array(currentDimensionLength)].map(() => {
89-
return typeof initialValues === "function"
90-
? initialValues()
91-
: initialValues;
92-
});
88+
const currentMatrix = [...Array(currentDimensionLength)];
9389

9490
const finalMatrix = needsRecursion
9591
? currentMatrix.map(() =>
9692
makeMatrix(remainingDimensions, initialValues)
9793
)
98-
: currentMatrix;
94+
: currentMatrix.map(() => {
95+
return typeof initialValues === "function"
96+
? initialValues()
97+
: initialValues;
98+
});
9999

100100
return finalMatrix;
101101
}

0 commit comments

Comments
 (0)