Skip to content

perf(modeling): O(n²) indexOf + splice in insertTjunctions.js #1425

@jbroll

Description

@jbroll

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 creation

Suggested 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions