Skip to content

Commit 210c18c

Browse files
committed
perf: create reducer cb
1 parent b52291c commit 210c18c

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/util/misc/matrix.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ export const multiplyTransformMatrices = (
8484
is2x2 ? 0 : a[1] * b[4] + a[3] * b[5] + a[5],
8585
] as TMat2D;
8686

87+
/**
88+
* @perf declare at top level to avoid recreating on the fly
89+
*/
90+
const reduceMatrices = (
91+
product: TMat2D | undefined | null | false,
92+
curr: TMat2D | undefined | null | false
93+
) =>
94+
curr && product ? multiplyTransformMatrices(curr, product) : curr || product;
95+
96+
/**
97+
* @perf declare at top level to avoid recreating on the fly
98+
*/
99+
const reduce2x2Matrices = (
100+
product: TMat2D | undefined | null | false,
101+
curr: TMat2D | undefined | null | false
102+
) =>
103+
curr && product
104+
? multiplyTransformMatrices(curr, product, true)
105+
: curr || product;
106+
87107
/**
88108
* Multiplies {@link matrices} such that a matrix defines the plane for the rest of the matrices **after** it
89109
*
@@ -97,13 +117,8 @@ export const multiplyTransformMatrixArray = (
97117
matrices: (TMat2D | undefined | null | false)[],
98118
is2x2?: boolean
99119
): TMat2D =>
100-
matrices.reduceRight(
101-
(product: TMat2D | undefined, curr) =>
102-
curr && product
103-
? multiplyTransformMatrices(curr, product, is2x2)
104-
: curr || product,
105-
undefined
106-
) || (iMatrix.concat() as TMat2D);
120+
matrices.reduceRight(is2x2 ? reduce2x2Matrices : reduceMatrices, undefined) ||
121+
(iMatrix.concat() as TMat2D);
107122

108123
export const calcPlaneRotation = ([a, b]: TMat2D) =>
109124
Math.atan2(b, a) as TRadian;

0 commit comments

Comments
 (0)