I would check out JSTS. This is a javascript port of a java library JTS that implements many operations on geometries.
Mainly the CascadedPolygonUnion operation looks like a good match with what you are trying to accomplish.