Geomap: legend enhancements

This seems like a lot unnecessary looping. Why keep looping through layerAttributes for each feature? Does each feature overwrite the previous? Seems like there is a lot of unnecessary processing.