- 主题:js的速度优化
这可不一定,这取决于实现。大量字符串拼接时,join 可以先把结果串的长度算出来才开始拷内容,避免反复重新分配内存。
当然现在 js 通常是直接用加号更快了。而 Python 至今仍然是 join 比加快。
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.
【 在 ottffsse (nothing) 的大作中提到: 】
: 因为 google developers错了。
: 直接拼字符串:
: 1)string是primitive type, which is simple
: ...................
--
修改:vonNeumann FROM 211.99.222.*
FROM 211.99.222.*
这种简单的场景自然是用 + 快,这有啥说的
比较 + 和 join 的效率,默认就是指下面两种写法在字符串比较长、数量比较多(如果字符串短数量少也没啥效率好关心的)的情况下的对比:
var arr = [];
for (.....) {
arr.push(..);
}
return arr.join();
var res = "";
for (....) {
res += ....;
}
return res;
【 在 ottffsse (nothing) 的大作中提到: 】
: ["a","b", , "c"].join() 相当于
: 一个Array实例的方法调用,首先要检查这个对象有没有这个方法,然后join方法要调用第0到第n-1个属性值的toString(),还要考虑被删掉或省略掉的0-(n-1)属性,怎么快呢?
: join如果把结果串的长度计算出来,是不是假设数组元素都是string类型的,还是调用toString之后算出来的?
: ...................
--
FROM 211.99.222.*
正宗
【 在 zeus2615 (zeuslord·呆猫) 的大作中提到: 】
: 我只想问下面这个英语正宗吗?
: 37 down vote accepted
: Internet Explorer is the only browser which really suffers from this in today's world. (Versions 5, 6, and 7 were dog slow. 8 does not show the same degradation.) What's more, IE gets slower and slower the longer your string is.
: ...................
--
FROM 211.99.222.*