Make creation of instrumented string more efficient

For #4350.

The original code for generating the instrumented HTML string rewrote the entire generated string each time a new ID was inserted, which created a lot of garbage when the HTML file was large. This new version keeps the original string around, and pulls out a chunk at a time to append to the generated string.