问题
I am now using ESLint prefer-template to force myself use template strings instead of string concatenation.
This made me think whether there is ever a need to use regular string over the template string format, e.g.
console.log('Why use this? It requires me to escape different quotes depending on the context. In this case \'.');
console.log(`When I can use this. It allows me to use all types of quotes (e.g. ', ") without ever worrying about escaping them.`);
I realise that JSPerf is not ideal profiling tool, though at least in the case of a static string, I cannot observe any performance penalty, e.g. http://jsperf.com/es-string-vs-template.
回答1:
You're right, there should not be any performance difference.
You just have to make sure to escape `
and ${
instead of '
or "
.
来源:https://stackoverflow.com/questions/32267017/performance-difference-between-a-regular-string-and-template-string