This type of calculation is pretty resource intensive. As you have noticed it works fine to a certain point, but the more data and longer the lines, the slower it becomes, especially when using JavaScript. To handle larger data sets you should consider doing this calculation on the server side, ideally in a spatial database where spatial indexing can be used. This would allow for significantly more complex route areas and more data points.
That said, if you must do this in JavaScript here are some optimization ideas you can try:
- Do an initial filtering of your data points so that you don't have to test all of them. A simple starting point is to get the bounding box of the route path, buffer it by your distance to account for places where the route line may be near an edge of the bounding box. Then do an initial filter of your data points. This should be fast since you have a much smaller polygon to compare against (simply min/max comparison of lat/lon). After that you can then do a comparison using your buffer polygon with just the filtered subset of points. A bounding box is likely to have a lot of irrelevant area depending on the shape of the route line, so you could potentially look at other shapes that might more tightly wrap the route path area roughly.
- Reduce the resolution of the generated buffer. I suspect that the generated buffer could have thousands, possibly tens of thousands of points. Lowering the resolution would create a margin of error, but would allow for the calculations to happen much faster. You could potentially buffer by a larger amount initially, reduce the resolution of the polygon, use that as an initial filter, then do a test with the high resolution buffer against the initially filtered results. A similar technique is often used in spatial databases for performance.
- Instead of doing a filter using a single bounding box, consider calculating a handful of bounding boxes along the route. You could start off with a grid, calculate all the cells the route path passes through. Then use those cells/bounding boxes as an initial filter. This would significantly reduce the number of points that would have to be checked against the high-resolution polygon. However, there would be tradeoffs. If you used too high of a resolution grid, you may end up doing a lot more calculations.
- Instead of using a polygon, calculate the actual straight-line distances from the points to route path, then do a simple check to see if that is within the desired distance. https://turfjs.org/docs/7.0.0/api/pointToLineDistance I'm not certain this will be faster. I have a feeling this solution would vary depending on the length of the line and number of points. It may be faster in some scenarios (maybe when you have long paths and lots of points).
- Similar to the above, you could break the route up into line segments (two lat/lon points per segment), then loop over these and the points and calculate the distance to your data set points. As soon as a point is within your distance range, you can skip checking the rest of the line segments.