Performance difference between a regular string and template string?

依然范特西╮ 提交于 2019-12-11 04:03:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!