Atlaslog
Atlaslog / Turf.js / Tutorial / Performance and large datasets
Turf.js · Tutorial

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.

Last verified 2026-11-03 · Turf.js 7.x · Cite this page
Key points
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
Runnable examplesChinese originalAll Tutorials
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). Performance and large datasets. https://www.jingweizhi.com/en/turf/tutorials/performance.html (accessed 2026-11-03).