Skip to content

Flatten polygon rings into Float64Arrays#122

Open
PontusHorn wants to merge 1 commit intomapbox:masterfrom
PontusHorn:perf-flat-polygon
Open

Flatten polygon rings into Float64Arrays#122
PontusHorn wants to merge 1 commit intomapbox:masterfrom
PontusHorn:perf-flat-polygon

Conversation

@PontusHorn
Copy link
Copy Markdown

@PontusHorn PontusHorn commented Apr 24, 2026

Pre-flattens polygon rings into contiguous Float64Arrays to speed up geometric computations and reduce iteration overhead. Improves performance of distance and centroid calculations by avoiding nested array structures.

Benchmarks show performance improvements across many different kinds of polygons, especially with complex polygons (40%+ for the "water1" and "water2" fixtures).

Not sure what level of browser support this project has, but Float64Array has been available across browsers since 2015 so it's very widespread.

These are my benchmark results from this branch:

Test Case                                          ops/sec        µs/op  iterations
------------------------------------------------------------------------------------------
water1 (complex, 23 rings, 4603 pts)                 338.4       2955.4  271
water2 (complex, 26 rings, 3091 pts)                 911.7       1096.8  730
triangle                                          523728.1          1.9  418983
square                                           3171694.4          0.3  2537364
circle-100                                        579260.0          1.7  463410
circle-1000                                        67530.0         14.8  54025
circle-10000                                        7142.4        140.0  5714
star-50                                            59973.1         16.7  47979
star-500                                           24208.4         41.3  19367
L-shape                                           184892.2          5.4  147919
polygon-with-hole                                    321.4       3111.2  258
dense-jagged-5000                                   3654.8        273.6  2924
water1-high-prec                                     299.5       3339.3  240
------------------------------------------------------------------------------------------
TOTAL (sum of µs/op)                                            10998.6

Versus master:

Test Case                                          ops/sec        µs/op  iterations
------------------------------------------------------------------------------------------
water1 (complex, 23 rings, 4603 pts)                 194.1       5151.8  156
water2 (complex, 26 rings, 3091 pts)                 526.8       1898.3  422
triangle                                          464813.0          2.2  371851
square                                           3021160.6          0.3  2416935
circle-100                                        298456.1          3.4  238766
circle-1000                                        33569.0         29.8  26856
circle-10000                                        3386.9        295.3  2710
star-50                                            43566.6         23.0  34854
star-500                                           15116.4         66.2  12094
L-shape                                           167280.5          6.0  133825
polygon-with-hole                                    247.3       4043.5  198
dense-jagged-5000                                   2341.6        427.1  1874
water1-high-prec                                     168.1       5950.5  135
------------------------------------------------------------------------------------------
TOTAL (sum of µs/op)                                            17897.0

Benchmark file:
benchmark.js

Pre-flattens polygon rings into contiguous Float64Arrays to speed up
geometric computations and reduce iteration overhead. Improves
performance of distance and centroid calculations by avoiding nested
array structures.

Benchmarks show performance improvements across many different kinds of
polygons, especially with complex polygons (40%+ for the "water1" and
"water2" fixtures).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant