Looping over all the tiles should only be needed to be done once to get the count and update the edges.
After that you can easily update the count as you add/remove tiles. You can also update the edges by only looking at the tiles around the tiles that changed.
If a high amount of tiles change then you may not want to update the edges around each one. Instead you could just update the edges of all the tiles on screen.
Outside of events, if you can do the update via JavaScript it would be way quicker with the loop. The trick is finding a way to accomplish it