【问题标题】:Performance difference between a regular string and template string?常规字符串和模板字符串之间的性能差异?
【发布时间】:2015-11-22 21:03:15
【问题描述】:

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

这让我想到是否需要在模板字符串格式上使用常规字符串,例如

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.

【问题讨论】:

  • 原来 ESLint quotes 规则已经支持“反引号”选项。

标签: javascript ecmascript-6 template-strings


【解决方案1】:

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

您只需要确保转义 `${ 而不是 '"

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 2012-03-10
    • 2023-03-10
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2015-06-28
    相关资源
    最近更新 更多