-
Notifications
You must be signed in to change notification settings - Fork 558
perf(modeling): O(n²) indexOf + splice in insertTjunctions.js #1425
Copy link
Copy link
Open
Description
Summary
insertTjunctions.js uses indexOf() followed by splice() in loops, causing O(n²) operations.
Usage Frequency: LOW
Only used by generalize() modifier, which is a specialized operation for cleaning up geometry.
Typical model usage: Rare - most models don't use generalize. Used mainly for:
- Cleaning up imported geometry
- Post-processing complex boolean results
Problem
File: packages/modeling/src/operations/modifiers/insertTjunctions.js
Lines: 70-82
idx = vertextag2sidestart.get(starttag).indexOf(sidetag) // O(n)
if (assert && idx < 0) throw new Error('assert failed')
vertextag2sidestart.get(starttag).splice(idx, 1) // O(n)Both indexOf() and splice() are O(n). When called in a loop processing many sides, this becomes O(n²).
Additionally, there's expensive string key generation:
const getTag = (vertex) => \`\${vertex}\` // Creates string each time
const starttag = getTag(vertex0)
const endtag = getTag(vertex1)
const newsidetag = \`\${starttag}/\${endtag}\` // More string creationSuggested Fix
Use a Set instead of array for O(1) operations:
// Instead of array with indexOf/splice:
const sides = new Set()
sides.add(sidetag)
sides.delete(sidetag) // O(1) instead of O(n)Impact
Low overall (rare operation), but when generalize is used on complex geometry it can be slow.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels