Skip to content

Commit 3d9d9f0

Browse files
jbrollclaude
andcommitted
perf(modeling): replace concat with push in extrudeFromSlices
Replace O(n²) array concatenation pattern with O(n) push operations. Each concat() creates a new array and copies all elements. In a loop with N iterations, this results in O(n²) total copying. Using push() with a for-loop avoids this overhead. Uses for-loop instead of spread operator to avoid stack limits with large polygon arrays (as noted by @hrgdavor in #1420). Fixes #1419 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 52be9a1 commit 3d9d9f0

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/modeling/src/operations/extrusions/extrudeFromSlices.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ const extrudeFromSlices = (options, base) => {
8181
if (edges.length === 0) throw new Error('the callback function must return slices with one or more edges')
8282

8383
if (prevSlice) {
84-
polygons = polygons.concat(extrudeWalls(prevSlice, currentSlice))
84+
const walls = extrudeWalls(prevSlice, currentSlice)
85+
for (let i = 0; i < walls.length; i++) {
86+
polygons.push(walls[i])
87+
}
8588
}
8689

8790
// save start and end slices for caps if necessary
@@ -95,17 +98,24 @@ const extrudeFromSlices = (options, base) => {
9598
if (capEnd) {
9699
// create a cap at the end
97100
const endPolygons = slice.toPolygons(endSlice)
98-
polygons = polygons.concat(endPolygons)
101+
for (let i = 0; i < endPolygons.length; i++) {
102+
polygons.push(endPolygons[i])
103+
}
99104
}
100105
if (capStart) {
101106
// create a cap at the start
102107
const startPolygons = slice.toPolygons(startSlice).map(poly3.invert)
103-
polygons = polygons.concat(startPolygons)
108+
for (let i = 0; i < startPolygons.length; i++) {
109+
polygons.push(startPolygons[i])
110+
}
104111
}
105112
if (!capStart && !capEnd) {
106113
// create walls between end and start slices
107114
if (close && !slice.equals(endSlice, startSlice)) {
108-
polygons = polygons.concat(extrudeWalls(endSlice, startSlice))
115+
const walls = extrudeWalls(endSlice, startSlice)
116+
for (let i = 0; i < walls.length; i++) {
117+
polygons.push(walls[i])
118+
}
109119
}
110120
}
111121
return geom3.create(polygons)

0 commit comments

Comments
 (0)