问题描述
今天,我阅读了这个线程关于字符串连接的速度.>
令人惊讶的是,字符串连接是赢家:
结果和我想的相反.此外,有很多文章对此进行了相反的解释,例如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转换最后是性能过载.
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:
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 havelarge 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.
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.
这篇关于为什么字符串连接比数组连接快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!