常规字符串和模板字符串之间的性能差异? [英] Performance difference between a regular string and template string?

查看:635
本文介绍了常规字符串和模板字符串之间的性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用ESLint prefer-template 强制自己使用模板字符串而不是字符串连接。

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.`);

我意识到JSPerf不是理想的分析工具,但至少在静态字符串的情况下,我无法观察到任何性能损失,例如 http://jsperf.com/es-string-vs-template

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.

推荐答案

你是对的,不应该有任何性能差异。

You're right, there should not be any performance difference.

你刚才必须确保逃避` $ {而不是'

You just have to make sure to escape ` and ${ instead of ' or ".

这篇关于常规字符串和模板字符串之间的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆