Improve performance calculating width

Measures each element on the first pass to stop repeated calls to
calculate_width interacting with the page.