简体   繁体   English

用于生成数组的字符串文字上的JavaScript String.split

[英]JavaScript String.split on a string literal to produce an array

I've seen a few javascript programmers use this pattern to produce an array: 我见过一些javascript程序员使用这个模式来生成一个数组:

"test,one,two,three".split(','); // => ["test", "one", "two", "three"]

They're not splitting user input or some variable holding a string value, they're splitting a hard-coded string literal to produce an array. 它们不是拆分用户输入或者是一个包含字符串值的变量,而是分割硬编码的字符串文字来生成数组。 In all of the cases I've seen a line like the above it would seem that it's perfectly reasonable to just use an array literal without relying on split to create an array from a string. 在所有的情况下,我已经看到如上所述的行,似乎只使用数组文字而不依赖于split来从字符串创建数组是完全合理的。 Are there any reasons that the above pattern for creating an array makes sense, or is somehow more efficient than simply using an array literal? 上述创建数组的模式是否有意义,或者仅仅使用数组文字更有效率?

When splitting a string at run-time instead of using an array literal, you are trading a small amount of execution time for a small amount of bandwidth. 在运行时拆分字符串而不是使用数组文字时,您需要为少量带宽交换少量执行时间。

In most cases I would argue that it is not worth it. 在大多数情况下,我认为这是不值得的。 If you are minifying and gzipping your code before publishing it, as you should be , using a single comma inside of a string versus a quote-comma-quote from two strings in an array would have little to no impact on bandwidth savings. 如果你在发布之前缩小和压缩代码, 就像你应该的那样 ,在字符串中使用单个逗号而不是数组中两个字符串的引号逗号引用对带宽节省几乎没有影响。 In fact after minification and gzipping, the version using the split string could possibly be longer due to the addition of the less compressible .split(',') . 事实上,在缩小和gzipping之后,由于添加了较少可压缩的.split(',')使用拆分字符串的版本可能会更长。

Splitting a string instead of creating an array literal of strings does mean a little less typing, but we spend more time reading code than writing it . 拆分字符串而不是创建字符串的数组文字确实意味着更少的输入,但我们花更多的时间阅读代码而不是编写代码 Using the array literal would be more maintainable in the future. 使用数组文字将来会更容易维护。 If you wanted to add a comma to an item in the array, you just add it as another string in the array literal; 如果要为数组中的项添加逗号,只需将其添加为数组文字中的另一个字符串; using split you would have to re-write the whole string using a different separator. 使用split你必须使用不同的分隔符重写整个字符串。

The only situation that I use split and a string literal to create an array is if I need an array that consists only of single characters, ie the alphabet, numbers or alphanumeric characters, etc. 我使用split和字符串文字来创建数组的唯一情况是,如果我需要一个只包含单个字符的数组,即字母,数字或字母数字字符等。

var letters = 'abcdefghijklmnopqrstuvwxyz'.split(''),
  numbers = '0123456789'.split(''),
  alphanumeric = letters.concat(numbers);

You'll notice that I used concat to create alphanumeric . 您会注意到我使用concat来创建alphanumeric If I had instead copy-pasted the contents of letters and numbers into one long string this code would compress better. 如果我将lettersnumbers的内容复制粘贴到一个长字符串中,则此代码将更好地压缩。 The reason I did not is that that would be a micro-optimization that would hurt future maintainability. 我不这样做的原因是,这将是一种微观优化,会损害未来的可维护性。 If in the future characters with accents, tildes or umlauts need to be added to the list of letters, they can be added in one place; 如果以后需要将带有重音符号,波浪号或变音符号的字符添加到字母列表中,可以将它们添加到一个地方; no need to remember to copy-paste them into alphanumeric too. 无需记住将它们复制粘贴到alphanumeric

Splitting a string may be useful for code golf , but in a production environment where minification and gzipping are factors and writing easily readable, maintainable code is important, just using an array literal is almost always the better choice. 拆分字符串对于代码高尔夫可能很有用,但在生产环境中,缩小和gzipping是因素并且编写易于阅读,可维护的代码很重要,仅使用数组文字几乎总是更好的选择。

For example, in ruby, the array ["test", "one", "two", "three"] 例如,在ruby中,数组["test", "one", "two", "three"]

could also wrote as %w(test one two three) , which save you some characters to type. 也可以写成%w(test one two three) ,这可以节省一些要输入的字符。

But javascript doesn't support such notation, so someone use split method to achieve it. 但是javascript不支持这种表示法,所以有人使用split方法来实现它。

If a large number a arrays are built manually. 如果大量数组是手动构建的。 It could potentially reduce the load time of your page since less characters are transmitted. 它可能会减少页面的加载时间,因为传输的字符较少。 But you would need a large number of arrays or large arrays to have a considerable difference. 但是你需要大量的数组或大型数组来产生相当大的差异。 Performance an array creation might be slower but it is faster to type. 创建数组的性能可能会慢一些,但输入速度会更快。 For large arrays I use a spreadsheet to apply the formating around each value with a formula like this ="'"&A1&"',". 对于大型数组,我使用电子表格来应用每个值的格式化,使用类似this =“'”&A1&“',”的公式。 I stick with the array literal. 我坚持使用数组文字。

It makes more sense to use the "split" method when you can't control the output, ie. 当你无法控制输出时,使用“拆分”方法更有意义,即。 user input or string output from a method. 用户输入或方法的字符串输出。 If you're trying to get a specific value in the output that is separated by something it is easier to use the split method. 如果您尝试在输出中获取由特定值分隔的特定值,则更容易使用split方法。 Of course, if you're controlling the values it doesn't always make sense. 当然,如果你控制价值,它并不总是有意义的。

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

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