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

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

问题描述

今天,我阅读了有关字符串连接速度的此主题

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.

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

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?

更新

因此,在现代浏览器中,字符串连接已经过优化,因此使用 + 符号比使用如果你想连接字符串,请加入。

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

@ Arthur指出,如果你真的希望加入字符串,那么 join 会更快分隔器。

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

推荐答案


浏览器字符串优化更改了字符串连接图片。

Browser string optimizations have changed the string concatenation picture.

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

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.

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

V8 javascript引擎(在谷歌浏览器中使用)使用此代码执行字符串连接:

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, "");
}

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

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天全站免登陆