JavaScript 有内置的 stringbuilder 类吗?
我看到一些代码项目解决方案。
但是JavaScript中有常规的实现吗?
I see a few The Code Project solutions.
But is there a regular implementation in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您必须为 Internet Explorer 编写代码,请确保您选择了使用数组连接的实现。在 IE 上,使用
+
或+=
运算符连接字符串的速度非常慢。对于 IE6 来说尤其如此。在现代浏览器上+=
通常与数组连接一样快。当我必须进行大量字符串连接时,我通常会填充一个数组并且不使用字符串生成器类:
请注意,
push
方法接受多个参数。If you have to write code for Internet Explorer make sure you chose an implementation, which uses array joins. Concatenating strings with the
+
or+=
operator are extremely slow on IE. This is especially true for IE6. On modern browsers+=
is usually just as fast as array joins.When I have to do lots of string concatenations I usually fill an array and don't use a string builder class:
Note that the
push
methods accepts multiple arguments.我刚刚重新检查了 http://jsperf.com/javascript-concat-vs- 的性能加入/2。
测试用例连接或连接字母表 1,000 次。
在当前的浏览器(Firefox、Opera、Internet Explorer 11 和 Chrome)中,“concat”比“join”快约 4-10 倍。
在 Internet Explorer 8 中,两者返回的结果大致相同。
不幸的是,在 Internet Explorer 7 中,“加入”速度大约快了 100 倍。
I just rechecked the performance on http://jsperf.com/javascript-concat-vs-join/2.
The test-cases concatenate or join the alphabet 1,000 times.
In current browsers (Firefox, Opera, Internet Explorer 11, and Chrome), "concat" is about 4-10 times faster than "join".
In Internet Explorer 8, both return about equal results.
In Internet Explorer 7, "join" is about 100 times faster unfortunately.
不,没有构建字符串的内置支持。您必须改用串联。
当然,您可以创建一个由字符串的不同部分组成的数组,然后对该数组调用 join(),但这取决于您使用的 JavaScript 解释器中如何实现连接。
我做了一个实验来比较
str1+str2
方法与array.push(str1, str2).join()
方法的速度。代码很简单:我在 Windows 7 x64 上的 Internet Explorer 8 和 Firefox 3.5.5 中对其进行了测试。
一开始,我测试了少量迭代(数百、数千项)。结果是不可预测的(有时字符串连接需要 0 毫秒,有时需要 16 毫秒,数组连接也是如此)。
当我将计数增加到 50,000 时,不同浏览器中的结果有所不同 - 在 Internet Explorer 中,字符串连接速度更快(94 毫秒),而连接速度更慢(125 毫秒),而在 Firefox 中,数组连接速度更快(113 毫秒)比字符串连接(117 毫秒)。
然后我将计数增加到 500'000。现在,在两种浏览器中,
array.join()
都比字符串连接慢:Internet Explorer 中的字符串连接为 937 毫秒,Firefox 中为 1155 毫秒,Internet 中的数组连接为 1265 毫秒;Explorer,Firefox 中为 1207 毫秒。我可以在 Internet Explorer 中测试而不会出现“脚本执行时间过长”的最大迭代次数为 850,000。然后 Internet Explorer 的字符串连接为 1593,数组连接为 2046,而 Firefox 的字符串连接为 2101,数组连接为 2249。
结果 - 如果迭代次数较少,您可以尝试使用
array.join()
,因为它在 Firefox 中可能会更快。当数量增加时,string1+string2
方法速度更快。更新
我在 Internet Explorer 6 (Windows XP) 上执行了测试。如果我尝试进行超过 100,000 次迭代的测试,该过程会立即停止响应并且永远不会结束。
在 40,000 次迭代中,结果为
这意味着 - 如果您需要支持 Internet Explorer 6,请选择
array.join()
,这比字符串连接快得多。No, there is no built-in support for building strings. You have to use concatenation instead.
You can, of course, make an array of different parts of your string and then call
join()
on that array, but it then depends on how the join is implemented in the JavaScript interpreter you are using.I made an experiment to compare the speed of
str1+str2
method versusarray.push(str1, str2).join()
method. The code was simple:I tested it in Internet Explorer 8 and Firefox 3.5.5, both on a Windows 7 x64.
In the beginning I tested on small number of iterations (some hundred, some thousand items). The results were unpredictable (sometimes string concatenation took 0 milliseconds, sometimes it took 16 milliseconds, the same for array joining).
When I increased the count to 50,000, the results were different in different browsers - in Internet Explorer the string concatenation was faster (94 milliseconds) and join was slower(125 milliseconds), while in Firefox the array join was faster (113 milliseconds) than string joining (117 milliseconds).
Then I increased the count to 500'000. Now the
array.join()
was slower than string concatenation in both browsers: string concatenation was 937 ms in Internet Explorer, 1155 ms in Firefox, array join 1265 in Internet Explorer, and 1207 ms in Firefox.The maximum iteration count I could test in Internet Explorer without having "the script is taking too long to execute" was 850,000. Then Internet Explorer was 1593 for string concatenation and 2046 for array join, and Firefox had 2101 for string concatenation and 2249 for array join.
Results - if the number of iterations is small, you can try to use
array.join()
, as it might be faster in Firefox. When the number increases, thestring1+string2
method is faster.UPDATE
I performed the test on Internet Explorer 6 (Windows XP). The process stopped to respond immediately and never ended, if I tried the test on more than 100,000 iterations.
On 40,000 iterations the results were
This means - if you need to support Internet Explorer 6, choose
array.join()
which is way faster than string concatenation.该代码看起来像是您想要采取的路线,但需要进行一些更改。
您需要将追加方法更改为如下所示。我已将其更改为接受数字 0,并使其返回
this
,以便您可以链接您的追加。That code looks like the route you want to take with a few changes.
You'll want to change the append method to look like this. I've changed it to accept the number 0, and to make it return
this
so you can chain your appends.JavaScript 的 StringBuilder 试试这个..
StringBuilder for JavaScript try this..
编辑
不,没有内置类。然而,字符串文字可能是一个合适的解决方法。
原始答案
JavaScript 的 ECMAScript 6 版本(又名 ECMAScript 2015)引入了 字符串文字。
请注意,用反引号而不是单引号将字符串括起来。
EDIT
No, there is not a built-in class. However,
string literals
may be a suitable work-around.ORIGINAL ANSWER
The ECMAScript 6 version (aka ECMAScript 2015) of JavaScript introduced string literals.
Notice that back-ticks, instead of single quotes, enclose the string.
下面是 TypeScript 中 StringBuilder 类的一个简单示例:
您可以像下面这样使用:
Here is a quick example of a StringBuilder class in TypeScript:
You can use like the following:
对于那些感兴趣的人,这里有一个调用 Array.join 的替代方法:
正如预期的那样,输出是字符串“foobar”。在 Firefox 中,此方法优于 Array.join,但优于 + 连接。由于 String.concat 要求将每个段指定为单独的参数,因此调用者受到执行 JavaScript 引擎施加的任何参数计数限制的限制。查看文档Function.prototype.apply() 了解更多信息。
For those interested, here's an alternative to invoking Array.join:
The output, as expected, is the string 'foobar'. In Firefox, this approach outperforms Array.join but is outperformed by + concatenation. Since String.concat requires each segment to be specified as a separate argument, the caller is limited by any argument count limit imposed by the executing JavaScript engine. Take a look at the documentation of Function.prototype.apply() for more information.
当我发现自己在 JavaScript 中进行了大量字符串连接时,我开始寻找模板。 Handlebars.js 工作得很好,使 HTML 和 JavaScript 更具可读性。 http://handlebarsjs.com
When I find myself doing a lot of string concatenation in JavaScript, I start looking for templating. Handlebars.js works quite well keeping the HTML and JavaScript more readable. http://handlebarsjs.com
在 C# 中你可以做类似的事情
在 JavaScript 中你可以做类似的事情
In C# you can do something like
In JavaScript you could do something like
使用sys.StringBuilder()。尝试一下文章Sys.StringBuilder Class 。
Use
sys.StringBuilder()
. Try the article Sys.StringBuilder Class.