There is a lot of object creation and appending which will cause a performance hit. Recommend instead building the whole legend as a string (no jQuery objects) and then append everything all at once (so only create a jQuery object once).