javascript 在 JS 中生成非常长的字符串(数十兆字节)的最有效方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3732101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most efficient way to generate a really long string (tens of megabytes) in JS
提问by zwol
I find myself needing to synthesize a ridiculously long string (like, tens of megabytes long) in JavaScript. (This is to slow down a CSS selector-matching operation to the point where it takes a measurable amount of time.)
我发现自己需要在 JavaScript 中合成一个长得可笑的字符串(比如几十兆字节)。(这是为了将 CSS 选择器匹配操作减慢到需要可测量的时间的程度。)
The best way I've found to do this is
我发现这样做的最好方法是
var really_long_string = (new Array(10*1024*1024)).join("x");
but I'm wondering if there's a more efficient way - one that doesn't involve creating a tens-of-megabytes array first.
但我想知道是否有更有效的方法 - 一种不涉及首先创建数十兆字节数组的方法。
采纳答案by oligofren
The accepted version uses String.prototype.concat()which is vastly slowerthan using the optimized string concatenating operator, +. MDN also recommendsto keep away from using it in speed critical code.
接受的版本使用String.prototype.concat()它比使用优化的字符串连接运算符慢得多,+. MDN 还建议避免在速度关键代码中使用它。
I have made three versionsof the above code to show the speed differences in a JsPerf. Converting it to using only using concatis only a third as fast as only using the string concatenating operator (Chrome - your mileage will vary). The edited version below will run twice as fast in Chrome
我制作了上述代码的三个版本来显示 JsPerf 中的速度差异。将其转换concat为仅使用的速度仅为仅使用字符串连接运算符的三分之一(Chrome - 您的里程会有所不同)。下面编辑过的版本在 Chrome 中的运行速度是原来的两倍
var x = "1234567890";
var iterations = 14;
for (var i = 0; i < iterations; i++) {
x += x+x;
}
回答by alex_1948511
For ES6:
对于 ES6:
'x'.repeat(10*1024*1024)
回答by deadrunk
This is the more efficient algorithm for generating very long strings in javascript:
这是在 javascript 中生成超长字符串的更有效算法:
function stringRepeat(str, num) {
num = Number(num);
var result = '';
while (true) {
if (num & 1) { // (1)
result += str;
}
num >>>= 1; // (2)
if (num <= 0) break;
str += str;
}
return result;
}
more info here: http://www.2ality.com/2014/01/efficient-string-repeat.html.
更多信息请访问:http: //www.2ality.com/2014/01/efficient-string-repeat.html。
Alternatively, in ECMA6 you can use String.prototype.repeat()method.
或者,在 ECMA6 中,您可以使用String.prototype.repeat()方法。
回答by Chuck
Simply accumulating is vastly faster in Safari 5:
在 Safari 5 中,简单的累加要快得多:
var x = "1234567890";
var iterations = 14;
for (var i = 0; i < iterations; i++) {
x += x.concat(x);
}
alert(x.length); // 47829690
Essentially, you'll get x.length * 3^iterationscharacters.
基本上,你会得到x.length * 3^iterations字符。
回答by Jacob
Not sure if this is a great implementation, but here's a general function based on @oligofren's solution:
不确定这是否是一个很好的实现,但这里有一个基于@oligofren 解决方案的通用函数:
function repeat(ch, len) {
var result = ch;
var halfLength = len / 2;
while (result.length < len) {
if (result.length <= halfLength) {
result += result;
} else {
return result + repeat(ch, len - result.length);
}
}
return result;
}
This assumes that concatenating a large string is faster than a series of small strings.
这假设连接一个大字符串比一系列小字符串快。

