为什么字符串连接比数组连接快? [英] Why is string concatenation faster than array join?

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

问题描述

今天,我阅读了这个线程关于字符串连接的速度.>

令人惊讶的是,字符串连接是赢家:

<块引用>

http://jsben.ch/#/OJ3vo

结果和我想的相反.此外,有很多文章对此进行了相反的解释,例如this.>

我可以猜测浏览器在最新版本中被优化为字符串 concat,但他们是如何做到的?可以说拼接字符串时使用+更好吗?


更新

因此,在现代浏览器中,字符串连接得到了优化,因此当您想要连接字符串时,使用 + 符号比使用 join 更快.​​

但是@Arthur 指出如果你真的想要join更快>加入字符串与分隔符.


更新 - 2020 年
Chrome:Array join 几乎是 2 倍快 是 String concat +请参阅:https://stackoverflow.com/a/54970240/984471

注意:

  • Array join 如果你有大字符串
  • 如果我们需要在最终输出中生成几个小字符串,最好使用string concat +,否则使用Array需要多次Array to String转换最后是性能过载.

解决方案

浏览器字符串优化改变了字符串连接图片.

Firefox 是第一个优化字符串连接的浏览器.从 1.0 版开始,数组技术实际上在所有情况下都比使用加号运算符慢.其他浏览器也对字符串连接进行了优化,因此 Safari、Opera、Chrome 和 Internet Explorer 8 也使用 plus 运算符显示出更好的性能.Internet Explorer 8 之前的版本没有这样的优化,所以数组技术总是比加号运算符快.

编写高效的 JavaScript:第 7 章 –更快的网站

V8 javascript 引擎(在 Google Chrome 中使用)使用此代码 进行字符串连接:

//ECMA-262,第 15.5.4.6 节函数 StringConcat() {如果 (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {throw MakeTypeError(" called_on_null_or_undefined", ["String.prototype.concat"]);}var len = %_ArgumentsLength();var this_as_string = TO_STRING_INLINE(this);如果 (len === 1) {返回 this_as_string + %_Arguments(0);}var 部分 = new InternalArray(len + 1);零件[0] = this_as_string;for (var i = 0; i < len; i++) {var 部分 = %_Arguments(i);部分[i + 1] = TO_STRING_INLINE(部分);}返回 %StringBuilderConcat(parts, len + 1, "");}

因此,他们在内部通过创建一个 InternalArray(parts 变量)来优化它,然后填充它.使用这些部分调用 StringBuilderConcat 函数.它很快是因为 StringBuilderConcat 函数是一些经过高度优化的 C++ 代码.在这里引用太长了,但在 runtime.cc 文件中搜索RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) 查看代码.

Today, I read this thread about the speed of string concatenation.

Surprisingly, string concatenation was the winner:

http://jsben.ch/#/OJ3vo

The result was opposite of what I thought. Besides, there are many articles about this which explain oppositely like this.

I can guess that browsers are optimized to string concat on the latest version, but how do they do that? Can we say that it is better to use + when concatenating strings?


Update

So, in modern browsers string concatenation is optimized so using + signs is faster than using join when you want to concatenate strings.

But @Arthur pointed out that join is faster if you actually want to join strings with a separator.


Update - 2020
Chrome: Array join is almost 2 times faster is String concat + See: https://stackoverflow.com/a/54970240/984471

As a note:

  • Array join is better if you have large strings
  • If we need generate several small strings in final output, it is better to go with string concat +, as otherwise going with Array will need several Array to String conversions at the end which is performance overload.

解决方案

Browser string optimizations have changed the string concatenation picture.

Firefox was the first browser to optimize string concatenation. Beginning with version 1.0, the array technique is actually slower than using the plus operator in all cases. Other browsers have also optimized string concatenation, so Safari, Opera, Chrome, and Internet Explorer 8 also show better performance using the plus operator. Internet Explorer prior to version 8 didn’t have such an optimization, and so the array technique is always faster than the plus operator.

Writing Efficient JavaScript: Chapter 7 – Even Faster Websites

The V8 javascript engine (used in Google Chrome) uses this code to do string concatenation:

// ECMA-262, section 15.5.4.6
function StringConcat() {
  if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
    throw MakeTypeError("called_on_null_or_undefined", ["String.prototype.concat"]);
  }
  var len = %_ArgumentsLength();
  var this_as_string = TO_STRING_INLINE(this);
  if (len === 1) {
    return this_as_string + %_Arguments(0);
  }
  var parts = new InternalArray(len + 1);
  parts[0] = this_as_string;
  for (var i = 0; i < len; i++) {
    var part = %_Arguments(i);
    parts[i + 1] = TO_STRING_INLINE(part);
  }
  return %StringBuilderConcat(parts, len + 1, "");
}

So, internally they optimize it by creating an InternalArray (the parts variable), which is then filled. The StringBuilderConcat function is called with these parts. It's fast because the StringBuilderConcat function is some heavily optimized C++ code. It's too long to quote here, but search in the runtime.cc file for RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) to see the code.

这篇关于为什么字符串连接比数组连接快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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