Improve performance of encodeHeader