ES6模板文字是否比字符串连接更快? [英] Are ES6 template literals faster than string concatenation?

查看:110
本文介绍了ES6模板文字是否比字符串连接更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人做过基准测试?如果HTML生成代码在Node和现代浏览器中使用字符串连接或模板文字,我很好奇。



例如:



字符串连接

 < body>+ 
< article>+
< time datetime ='+ date.toISOString()+'>+ date +< / time>+
article>+
< / body>

模板文字

 `<身体GT; 
< article>
< time datetime ='$ {date.toISOString()}'> $ {date}< / time>
< / article>
< / body>`


解决方案

似乎此时字符串连接更快 http://jsperf.com/ es6-string-literals-vs-string-concatenation

  ES6与变量19,992,512 ±5.21%78%更低
带变量的字符串连接89,791,408±2.15%最快
ES6功能461,358±3.12%99%慢
字符串连接功能503,255±1.77%99%慢

我测试的是运行在Chrome 43.0.2334.0金丝雀(64位),它正在使用V8 4.3.31 ,启用#enable-javascript-harmony 标志。



为了参考,Node.js上的最新版本(写入时为0.12.0)正在使用V8 3.28.73:https://raw.githubusercontent.com/joyent/node/master/ChangeLog



我确定可能应用的所有可能的性能优化尚未应用,因此,随着ES6更接近完成,这些功能将被迁移到稳定的分支,希望性能更好的是合理的。


Has anyone done benchmarks? I'm curious if HTML generation code is faster with string concatenation or with template literals in Node and modern browsers.

For example:

String concatenation

"<body>"+
  "<article>"+
    "<time datetime='" + date.toISOString() +"'>"+ date +"</time>"+
  "</article>"+
"</body>"

Template literal

`<body>
  <article>
    <time datetime='${ date.toISOString() }'>${ date }</time>
  </article>
</body>`

解决方案

It seems for the moment string concatenation is faster: http://jsperf.com/es6-string-literals-vs-string-concatenation

ES6 with variable                     19,992,512    ±5.21%    78% slower
String concatenation with variable    89,791,408    ±2.15%    fastest
ES6 with function                     461,358       ±3.12%    99% slower
String concatenation with function    503,255       ±1.77%    99% slower

I tested was run on Chrome 43.0.2334.0 canary (64-bit), which is using V8 4.3.31, with the #enable-javascript-harmony flag enabled.

For reference, the latest version on Node.js (0.12.0 at the time of writing) is using V8 3.28.73: https://raw.githubusercontent.com/joyent/node/master/ChangeLog

I'm sure all the possible performance optimizations that could be applied have not been applied yet, so it would be reasonable to expect performance to get better as ES6 gets closer to finalization and these features get migrated to the stable branch.

这篇关于ES6模板文字是否比字符串连接更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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