Skip to content

Migrate @turf/polygonize to Map/Set - #3074

Merged
mfedderly merged 3 commits into
masterfrom
mf/polygonize-mapset
Jun 22, 2026
Merged

Migrate @turf/polygonize to Map/Set#3074
mfedderly merged 3 commits into
masterfrom
mf/polygonize-mapset

Conversation

@mfedderly

Copy link
Copy Markdown
Collaborator

When looking at #3045 it occurred to me that this code probably hasn't been updated since we moved from ES5 to ES8 support in Turf. Specifically: Node was getting a very log string assigned as its id, and then we were using an Object keyed by those strings to do lookups. These are both suspiciously expensive so I figured I'd take a look.

This changes Node's id to a number (incremented as Graph creates them). Graph keeps both a Set<Node> and an index of Map<longitude, Map<latitude, Node>> used to quickly figure out if a Node has already been created for a given set of coordinates for reuse.

Some benchmarks (M4 Macbook Pro, on battery) shows a pretty nice improvement overall:

Before:
complex x 2,520 ops/sec ±0.42% (98 runs sampled)
cutedge x 216,103 ops/sec ±0.22% (95 runs sampled)
dangle x 412,099 ops/sec ±0.35% (95 runs sampled)
kinked-linestring x 315,924 ops/sec ±0.25% (99 runs sampled)
linestrings x 228,949 ops/sec ±0.18% (97 runs sampled)
multi-linestring x 393,533 ops/sec ±0.20% (98 runs sampled)
two-polygons x 224,014 ops/sec ±0.22% (97 runs sampled)

After this PR:
complex x 4,511 ops/sec ±0.34% (98 runs sampled)
cutedge x 266,407 ops/sec ±0.24% (100 runs sampled)
dangle x 530,973 ops/sec ±0.28% (97 runs sampled)
kinked-linestring x 383,615 ops/sec ±0.17% (98 runs sampled)
linestrings x 280,813 ops/sec ±3.76% (93 runs sampled)
multi-linestring x 441,141 ops/sec ±2.94% (90 runs sampled)
two-polygons x 262,369 ops/sec ±0.13% (98 runs sampled)

When looking at #3045 it occurred to me that this code probably hasn't been updated since we moved from ES5 to ES8 support in Turf.
Specifically: Node was getting a very log string assigned as its id, and then we were using an Object keyed by those ids to do lookups.

This changes Node's id to a number (incremented as Graph creates them). Graph keeps both a Set<Node> and an index of Map<longitude, Map<latitude, Node>> used
to quickly figure out if a Node has already been created for a given set of coordinates for reuse.
@mfedderly mfedderly added this to the v7.4 milestone Jun 22, 2026
@mfedderly
mfedderly marked this pull request as ready for review June 22, 2026 00:29
@mfedderly
mfedderly merged commit 263d357 into master Jun 22, 2026
5 checks passed
@mfedderly
mfedderly deleted the mf/polygonize-mapset branch June 22, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant