This are char comparisions, but in lua they are expressed as full string compare. Compare as bytes instead, this is significantly faster, especially with luajit compiled code.
mp.utils.terminal_display_width