Performance and large datasets
Quick answer
This tutorial explains why Turf's synchronous pure-JavaScript calls slow down around a hundred thousand features, and how to scale: bbox pre-filtering before booleanPointInPolygon, moving buffer and intersect into a Web Worker, batching work with requestAnimationFrame, and choosing between mutate: true and turf.clone. It includes a scale table from ten thousand to millions of features. You will be able to keep the main thread responsive.
Key points
- Pre-filter candidate coordinates against turf.bbox before calling booleanPointInPolygon on complex polygons.
- Run expensive turf.buffer or turf.intersect calls inside a Worker via importScripts of turf.min.js.
- Use mutate: true to skip deep copies when the source data will not be reused.
Example
var zone = turf.polygon([/* 上百个顶点 */]);
var box = turf.bbox(zone); // [西, 南, 东, 北]
function inBox(lng, lat) {
return lng >= box[0] && lng <= box[2] &&
lat >= box[1] && lat <= box[3];
}
var candidates = [];
turf.coordEach(pointsFC, function (coord) {
if (inBox(coord[0], coord[1])) candidates.push(coord);
});
var hits = [];
for (var i = 0; i < candidates.length; i++) {
if (turf.booleanPointInPolygon(candidates[i], zone)) {
hits.push(candidates[i]);
}
}
console.log('候选', candidates.length, '命中', hits.length);var worker = new Worker('geo-worker.js');
worker.onmessage = function (e) {
if (e.data.type === 'buffer') {
map.getSource('zone').setData(e.data.result);
}
};
worker.postMessage({
type: 'buffer',
center: [116.391263, 39.907359],
radiusKm: 1.2
});
importScripts('https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js');
self.onmessage = function (e) {
var msg = e.data;
if (msg.type !== 'buffer') return;
var pt = turf.point(msg.center);
var zone = turf.buffer(pt, msg.radiusKm, { units: 'kilometers' });
self.postMessage({ type: 'buffer', result: zone });
};
Notes
- Calling turf functions inside map.on('mousemove') or animation loops drops frame rates to single digits.
- Batched results must be null-checked, since some intersect calls return null and others return GeometryCollection.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Performance and large datasets. https://www.jingweizhi.com/en/turf/tutorials/performance.html (accessed 2026-11-03).