Search code examples
javascriptjsonleaflettime-complexitygeojson

What is the time complexity of leaflet function onEachFeature function here


What is the complexity of the leaflet function onEachFeature in general? is it the same as the complexity of a for loop when it comes to processing each key in a big geojson file?


Solution

  • onEachFeature is a user-defined function, and thus its algorithmical complexity cannot be known beforehand.

    It does get called once per GeoJSON Feature on initialization of a L.GeoJSON instance, and whenever the .addData() method of a L.GeoJSON is called.

    Also take into account that instantiating any L.Circle, L.Polyline or L.Polygon has a complexity of O(n*log(n)), where n is the number of points in the geometry, due to usage of a Douglas-Peucker simplification.

    Thus, complexity of adding features to a L.GeoJSON should be of a magnitude like

     O( m * (n*log(n) + f ) )
    

    Where m is the number of features, n is the number of points/vertices per feature, and f is the complexity of the user-defined onEachFeature function.