Skip to content

Commit 104f994

Browse files
rexxarsmfedderly
andauthored
perf(boolean-intersects): replace redundant flattenEach with booleanDisjoint (#3103)
`booleanDisjoint` already compares every flattened geometry pair internally. I also removed @turf/meta and @turf/helpers from the package's dependencies since nothing in the package imports them anymore (helpers was unused even before this change). Benchmarks show between +50% and +100% improvement in speed on MacBook M4 Pro. Co-authored-by: mfedderly <24275386+mfedderly@users.noreply.github.com>
1 parent 3a04c3a commit 104f994

3 files changed

Lines changed: 5 additions & 22 deletions

File tree

packages/turf-boolean-intersects/index.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Feature, Geometry } from "geojson";
22
import { booleanDisjoint } from "@turf/boolean-disjoint";
3-
import { flattenEach } from "@turf/meta";
43

54
/**
65
* Boolean-intersects returns (TRUE) if the intersection of the two geometries is NOT an empty set.
@@ -36,18 +35,9 @@ function booleanIntersects(
3635
ignoreSelfIntersections?: boolean;
3736
} = {}
3837
) {
39-
let bool = false;
40-
flattenEach(feature1, (flatten1) => {
41-
flattenEach(feature2, (flatten2) => {
42-
if (bool === true) {
43-
return true;
44-
}
45-
bool = !booleanDisjoint(flatten1.geometry, flatten2.geometry, {
46-
ignoreSelfIntersections,
47-
});
48-
});
49-
});
50-
return bool;
38+
// booleanDisjoint already compares every flattened geometry pair, so
39+
// intersects is simply its negation.
40+
return !booleanDisjoint(feature1, feature2, { ignoreSelfIntersections });
5141
}
5242

5343
export { booleanIntersects };

packages/turf-boolean-intersects/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"contributors": [
77
"Rowan Winsemius <@rowanwins>",
88
"Denis Carriere <@DenisCarriere>",
9-
"David Whittingham <@01100100>"
9+
"David Whittingham <@01100100>",
10+
"Espen Hovlandsdal <@rexxars>"
1011
],
1112
"license": "MIT",
1213
"bugs": {
@@ -67,8 +68,6 @@
6768
},
6869
"dependencies": {
6970
"@turf/boolean-disjoint": "workspace:*",
70-
"@turf/helpers": "workspace:*",
71-
"@turf/meta": "workspace:*",
7271
"@types/geojson": "catalog:",
7372
"tslib": "catalog:"
7473
}

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)