Skip to content

Commit 0e71a3d

Browse files
committed
Speed up the hot vector operations
* Add a "fast path" for `add` and `subtract` when there are just two vectors involved * Remove destructuring of the vector parameters into components This requires a copy of the values, which we can avoid by using an indexed access into the vector itself.
1 parent 70e4d78 commit 0e71a3d

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/vector.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export function randomVector(min = 0, max = 1): Vector {
1111
return [random(min, max), random(min, max), random(min, max)];
1212
}
1313

14-
export function negate([e0, e1, e2]: Vector): Vector {
15-
return [-e0, -e1, -e2];
14+
export function negate(v: Vector): Vector {
15+
return [-v[0], -v[1], -v[2]];
1616
}
1717

1818
export function translate(v: Vector, v2: Vector): Vector {
@@ -38,8 +38,8 @@ export function length(v: Vector): number {
3838
return Math.sqrt(lengthSquared(v));
3939
}
4040

41-
export function lengthSquared([e0, e1, e2]: Vector): number {
42-
return e0 * e0 + e1 * e1 + e2 * e2;
41+
export function lengthSquared(v: Vector): number {
42+
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
4343
}
4444

4545
export function multiplyInline(v: Vector, other: Vector): Vector {
@@ -51,30 +51,42 @@ export function multiplyInline(v: Vector, other: Vector): Vector {
5151

5252
// Utilities
5353

54-
export function add(v: Vector, ...other: Vector[]): Vector {
55-
const result: Vector = [...v];
56-
for (let i = 0; i < other.length; i++) {
57-
result[0] += other[i][0];
58-
result[1] += other[i][1];
59-
result[2] += other[i][2];
54+
export function add(v: Vector, other: Vector, ...others: Vector[]): Vector {
55+
const result: Vector = [v[0] + other[0], v[1] + other[1], v[2] + other[2]];
56+
if (others.length === 0) {
57+
return result;
58+
}
59+
60+
for (let i = 0; i < others.length; i++) {
61+
result[0] += others[i][0];
62+
result[1] += others[i][1];
63+
result[2] += others[i][2];
6064
}
6165
return result;
6266
}
63-
export function subtract(v: Vector, ...other: Vector[]): Vector {
64-
const result: Vector = [...v];
65-
for (let i = 0; i < other.length; i++) {
66-
result[0] -= other[i][0];
67-
result[1] -= other[i][1];
68-
result[2] -= other[i][2];
67+
export function subtract(
68+
v: Vector,
69+
other: Vector,
70+
...others: Vector[]
71+
): Vector {
72+
const result: Vector = [v[0] - other[0], v[1] - other[1], v[2] - other[2]];
73+
if (others.length === 0) {
74+
return result;
75+
}
76+
77+
for (let i = 0; i < others.length; i++) {
78+
result[0] -= others[i][0];
79+
result[1] -= others[i][1];
80+
result[2] -= others[i][2];
6981
}
7082
return result;
7183
}
7284
export function multiply(v1: Vector, v2: Vector): Vector {
7385
return [v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]];
7486
}
7587

76-
export function scaled([e0, e1, e2]: Vector, n: number): Vector {
77-
return [e0 * n, e1 * n, e2 * n];
88+
export function scaled(v: Vector, n: number): Vector {
89+
return [v[0] * n, v[1] * n, v[2] * n];
7890
}
7991
export function unscaled(v: Vector, n: number): Vector {
8092
return scaled(v, 1 / n);

0 commit comments

Comments
 (0)