简体   繁体   English

JavaScript 字符串拆分:固定宽度与分隔性能

[英]JavaScript String split: fixed-width vs. delimited performance

I am building a string to be parsed into an array by JavaScript.我正在构建一个字符串,由 JavaScript 解析为一个数组。 I can make it delimited or I can make the fields fixed-width.我可以将其分隔,也可以将字段设为固定宽度。 To test it, I built this jsperf test using a data string where the fields are both fixed-width and comma-delimited:为了测试它,我使用数据字符串构建了这个 jsperf 测试,其中字段都是固定宽度和逗号分隔的:

https://jsperf.com/string-split-fixed https://jsperf.com/string-split-fixed

I have only tested on Windows with Firefox and Chrome, so please run the test from other OSes and browsers.我只在 Windows 上使用 Firefox 和 Chrome 进行过测试,所以请在其他操作系统和浏览器上运行测试。 My two test results are clear: String.prototype.split() is the winner by a large margin.我的两个测试结果很清楚: String.prototype.split()是赢家。

Is my fixed-width code not efficient enough, or is the built-in string split function simply superior?是我的固定宽度代码不够高效,还是内置的字符串拆分功能只是优越? Is there a way to code it so that the fixed-width parsing triumphs?有没有办法对其进行编码,以便固定宽度的解析成功? If this was C/C++, the fixed-width code, written properly, would be the clear winner.如果这是 C/C++,那么正确编写的固定宽度代码将是明显的赢家。 But I know JavaScript is an entirely different beast.但我知道 JavaScript 是一个完全不同的野兽。

String.prototype.split() is a built-in JavaScript function. String.prototype.split()是一个内置的 JavaScript 函数。 Expect it to be highly optimized for the particular JS engine and be written not in JavaScript but in C++.期望它针对特定的 JS 引擎进行高度优化,并且不是用 JavaScript 而是用 C++ 编写。

It should thus not come a surprise that you can't match its performance with pure JavaScript code.因此,您无法将其性能与纯 JavaScript 代码相匹配也就不足为奇了。

String operations like splitting a delimited string are inherently memory-bound.字符串操作(如拆分分隔字符串)本质上是受内存限制的。 Hence, knowing the location of delimiters doesn't really help much, since the entire string still needs to be traversed at least once (to copy the delimited fragments).因此,知道分隔符的位置并没有多大帮助,因为整个字符串仍然需要至少遍历一次(以复制分隔的片段)。 Fixed-position splitting might be faster for strings that exceed D-cache size, but your string is just 13KB long, so traversing it multiple times isn't going to matter.对于超过 D-cache 大小的字符串,固定位置拆分可能会更快,但您的字符串只有 13KB 长,因此多次遍历它无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM