Rendering Leaflet clusters fast and dynamically – let’s compare 3 methods

Maps in front-end development

At some point in the career of every front-end developer, there will come a time when they will face the task of map implementation. There are many uses of maps in applications. Today I will focus on maps that contain a large number of points with specific coordinates. They can be, for example, delivery points, restaurants, shops, etc.

Leaflet is a standard solution that developers often use due to its open-source nature. Today we will talk about it.
Let’s imagine now that our task is to add specific points to the map, but we do not add it only once after launching the map, but we send a request for points each time you move around the map (moving or changing the zoom level). Sounds familiar?

Leaflet clusters in action

If so, you’ve probably come across the recommended Leaflet plugin, Leaflet.markercluster. In the documentation, we can find examples where 10,000 or even 50,000 points are implemented! Furthermore, thanks to chunkedLoading, we can display many points on the map in a relatively user-friendly way. Below you will find an example of Leaflet clusters from the official Github page of the project:

Great, the plugin can handle significant challenges, so it will dynamically add points, right? Not so fast! Yes, Leaflet.markercluster is doing quite well, and this is also the solution I introduced in my recent project, but the key to success, in this case, is knowledge.

Now imagine this situation: at the beginning, you add 350 points to the map. Then, the user moves the map – we have a new map area, so we send another request for new points and generate 350 points on the map again. It sounds simple, but it is not. Because as you will find out – this operation takes a long time. Not tens of milliseconds, not even several hundred. It takes 2-3 seconds, and sometimes even more!

I don’t even want to think about what numbers we can achieve with a slow internet connection. It happens every time you move the map or change the zoom! The more logic related to the map (added, e.g., search engine, custom marker icons, etc.), the more these results can be depressing.

You are probably thinking: why add 350 points each time if we can remember those already there and add only those that have not been on the map so far. Additionally, you can delete those markers that are not in the map’s visible area, right? Sounds wonderful 🙂 However, this type of operation, in the case of Leaflet.markercluster, can be a suicide!

Solution to Leaflet.markercluster performance issues

Our team searched the entire internet for answers, trying to handle the optimization of our map. We had many custom solutions there (like a search engine and a lot of custom logic), but they were not the source of the problem as it turned out. Finally, one day I came across a fascinating note by danzel, who explained the situation in detail.
The original post you can find here:

It was like finding the Holy Graal!

As you’ve probably noticed, when you dynamically change points on the map, the quickest solution is to … remove all points and re-apply them! Even those that are already on the map! No selection. Clean and insert. Why?

Leaflet clusters methods comparison

All thanks to the efficiency of specific Leaflet.markercluster methods. You can find a detailed comparison of them below. I installed 10000 points for the tests and set them to change after moving the map, and I did sandboxes for three different cases. I didn’t add additional logic to remove clusters out of bounds. I keep it simple in these examples and compare only add/remove methods.

You can see the results in ms in the console:

removeLayer combined with addLayer



leaflet clusters with removeLayer and addLayer methods

removeLayers combined with addLayer



leaflet clusters with removeLayers and addLayer methods

clearLayers combined with addLayers



leaflet clusters with clearLayers and addLayers methods

As you can notice, clearLayers combining with addLayers are the two fastest methods, and I strongly recommend using them in your project!

The results are fascinating, and as you can see, the most obvious solutions are not always the best. By changing the method of adding and removing markers on the map, we have improved its performance more than 10 times! Keep the above scores in mind if you will implement Leaflet with Leaflet.markercluster. It can save you many hours of searching for a solution.

4 replies on “Rendering Leaflet clusters fast and dynamically – let’s compare 3 methods”

  • celebrants in australia
    May 14, 2021 at 5:03 pm

    Wow, wonderful weblog layout! How long have
    you ever been blogging for? you make running a
    blog glance easy. The entire glance of your web
    site is fantastic, as smartly as the content material!

    • May 14, 2021 at 6:39 pm

      Thank you for your kind message. I’m blogging for two months. Have a nice day!

  • INC
    May 14, 2021 at 8:44 pm

    I am really enjoying the theme/design of your website. Do
    you ever run into any browser compatibility issues?
    A few of my blog readers have complained about
    my website not operating correctly in Explorer but looks
    great in Opera. Do you have any solutions to help fix this problem?

    • May 16, 2021 at 4:18 pm

      Hello, sorry to hear that you have problems with Internet Explorer, but I have to tell You that this is a very outdated browser and it will be only worse because Microsoft won’t update it anymore. You can, of course, fix a lot of problems on Internet Explorer, but You have to specify it.

Leave a Reply

Your email address will not be published. Required fields are marked *