react-native-better-clustering
Drop-in clustered MapView for react-native-maps. Clusters are computed in C++ via a Nitro
HybridObject; every visible bubble/leaf is still a React react-native-maps <Marker>.
Source is truth. Where the library README and the code disagree, follow the code.
Paths like package/src/... are relative to a library checkout, not this skills repo.
Version verification
Package: react-native-better-clustering
Version: 1.0.0
Commit: 7c7d659046a3d1bb8b7d4cb06a94f6e0d509862e
Verification date: 2026-08-21npm view react-native-better-clustering version
node -p "require('react-native-better-clustering/package.json').version"If your version differs, re-read node_modules/react-native-better-clustering/src/ before trusting
internals.
When to use
- Installing / configuring clustering on a React Native map
- Migrating from
react-native-map-clusteringorreact-native-clusterer - Thousands of markers, pan/zoom jank, flicker, wrong membership
- Native crash or Nitro load failure involving this package
- Aggregating cluster values (
clusterProperties) - Changing the C++ engine, Nitro spec, or React layer
- Reviewing any performance claim about clustering / marker FPS
When NOT to use
| Situation | Instead |
|---|---|
| Base map / tiles / camera / gestures | Load the react-native-maps skill |
react-native-better-maps |
Load that skill — different library, native clustering |
Mapbox / MapLibre / @rnmapbox/maps |
Those SDKs cluster internally |
| Non-geographic clustering | This engine is lon/lat Web Mercator only |
Core architecture
- Compute is C++; drawing is React.
ClusterEngineCorebuilds the hierarchy; the defaultMapViewmounts one RN Maps<Marker>per visible node. - One HybridObject, not a map view. Nitro autolinks
ClusterEngine→HybridClusterEngine. - Load copies; query converts.
packPoints→setPointsmemcpy → syncgetClustersthat allocates one JS object per node. - Rebuilds are all-or-nothing. No incremental insert API;
dataidentity change destroys and rebuilds. - Region sync is throttled (
clusterUpdateIntervalMsdefault100).
Details: architecture.md.
Thread safety (CRITICAL)
ClusterEngineCore is NOT thread-safe.
No mutex / lock / atomic protects build state.
Do not issue concurrent buildAsync / getClusters / getChildren / getLeaves / isBuilt
against the same engine instance.
Supercluster lifecycle gating is incidental protection only.
createClusterEngine() (headless) provides no such guarantee.buildAsync moves work off the JS thread — that is not concurrent-query safety.
For continuous querying during a rebuild, build a second engine fully, then swap.
→ architecture.md
API guidance
| Import | Surface |
|---|---|
react-native-better-clustering |
Drop-in clustered MapView |
.../hooks |
useClusterer — you own the MapView |
.../engine |
Supercluster, createClusterEngine — headless |
.../geojson |
Feature helpers |
.../utils |
packPoints, etc. |
.../clusterer |
Clusterer component |
clusterProperties is NOT a root MapView prop. Aggregation exists only on /engine and
/hooks (sum | min | max only). Surface: api.md. Deep detail:
aggregation.md.
Problem → reference routing
| Problem | Start here |
|---|---|
| Concurrent engine / headless threading | architecture.md |
| Installation & setup | setup.md |
| Blank Android map | setup.md |
| Basic usage / which API | api.md |
| Slow / "make it 2× faster" | Canonical optimization → performance.md |
| 10k+ markers / ~20 FPS pan | performance.md |
| Marker re-renders / flicker | rendering.md |
| Wrong membership / viewport edge | algo.md |
Aggregation (clusterProperties) |
aggregation.md + api.md |
| Migration | migration.md |
| Regression / PR slower | performance.md |
| Debugging unknown | debugging.md |
| Platform iOS / Android | setup.md, debugging.md |
| Testing / contributing | contributing.md |
Canonical optimization workflow
1. Establish baseline
2. Define workload
3. Profile
4. Identify bottleneck
5. Form hypothesis
6. Smallest change
7. Benchmark
8. Compare before vs after
9. Add regression coverageVague "2× faster" → reframe first: point count, distribution, pan/zoom/load, platform, device, debug vs release, metric, current baseline. Profiling: performance.md.
Do not rewrite the KD-tree without a profile, add memoization that does not stabilize identity, or claim FPS without measuring release builds on a real device.
Common mistakes
- Treating clustering FPS as engine FPS — visible RN Maps markers are usually the ceiling
- Concurrent
buildAsync+ query on one headless engine - Passing
clusterPropertiesto rootMapView - Claiming "zero-copy" / "60 FPS at 10k" — not supported by source
- Optimising without a baseline workload
- Assuming Jest covers C++ clustering (it mocks Nitro)
Troubleshooting
- Name the layer: compute / JSI / React commit / RN Maps annotations → performance.md
- Name the trigger: dataset identity, region event, settle, mount
- Measure on the failing platform → performance.md
- Then fix → debugging.md
Decision tree and real error strings live in the debugging reference.
Known limitations
| Limitation | Consequence |
|---|---|
| Rendering is RN Maps markers | Visible count, not point count, sets FPS ceiling |
ClusterEngineCore not thread-safe |
Serialize build/query per instance |
| No incremental updates | data identity change rebuilds everything |
clusterProperties not on root MapView |
Use /hooks or /engine |
| New Architecture + no Expo Go | RN 0.78+; needs a development build |
| No CI performance gate | Regressions need your own measurement |
Accuracy
Label claims SOURCE FACT / TEST FACT / BENCHMARK FACT / EXAMPLE FACT / INFERENCE. Banned as fact: "zero-copy", "zero-bridge", "no JS overhead", "10k markers at 60 FPS".
2D KD-tree range/within complexity is typically O(√n + k) (not O(log n + k)). Build walks
current in array/ingest order; the tree answers neighbour queries only.