3 ms·
Except it’s more if I have text following a variable. Try this: STR.”x=\{x} y=\{y} z=\{z}.” Compared to: “x=“ + x + “ y=“ + y + “ z=“ + z + “.” A s
by quacker 3y ago
Except it’s more if I have text following a variable.
Try this:
STR.”x=\{x} y=\{y} z=\{z}.”
Compared to:
“x=“ + x + “ y=“ + y + “ z=“ + z + “.”
A string template incurs an overhead of 3 characters per variable (“\“, “{“, and “}”) plus the leading “STR.”, which is 4 characters. Concatenation incurs an overhead of 4 non-whitespace characters per variable (`”+` and `+”`), and that’s ignoring whitespace. If you have more than 3-4 variables, string templates are shorter.
The concatenation example is less readable as well. Have I included whitespace to exactly match the string template example? It’s a little hard to tell at a glance because I have to distinguish what is code whitespace and what’s included in the string.