JavaScript String.split for a string literal to create an array

I saw how several javascript programmers used this template to create an array:

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

They do not share user input or any variable containing a string value; they break the string literal of a string encoding to create an array. In all cases, when I saw a string similar to the one above, it would seem quite reasonable to just use an array literal without relying on split to create an array from the string. Are there reasons why the above pattern for creating an array makes sense or is it somehow more efficient than just using an array literal?

+6
source share
4 answers

When splitting a string at runtime instead of using an array literal, you trade a small amount of runtime for a small amount of bandwidth.

In most cases, I would say that it is not worth it. If you used up and downloaded your code before publishing it, as it should be , using a single comma inside a line compared to a quote-comma- a quote from two lines in an array is unlikely to affect bandwidth savings. In fact, after minimizing and gzipping, the split-string version may be longer due to the addition of less compressible .split(',') .

Splitting a string instead of creating an array of string literals means a bit less text input, but we spend more time reading code than writing it . Using an array literal will be more convenient in the future. If you want to add a comma to an array element, you simply add it as another string to the array literal; using split , you will have to rewrite the entire line using another separator.

The only situation when I use split and a string literal to create an array is if I need an array consisting of only single characters, i.e. an alphabet, numbers or alphanumeric characters, etc.

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

You will notice that I used concat to create alphanumeric . If I instead copied the contents of letters and numbers into one long line, this code would compress better. The reason why I did not do this is because it will be micro-optimization, which will damage future maintainability. If in the future characters with accents, tildes or umlauts are to be added to the list of letters, they can be added in one place; no need to remember to copy them to alphanumeric too.

Splitting a string can be useful for golf code , but in a production environment where minimization and gzipping are factors and writing easy to read, maintainable code is important, just using an array literal is almost always the best choice.

+1
source

For example, in ruby ​​array ["test", "one", "two", "three"]

could also write like %w(test one two three) , which will save you some characters for input.

But javascript does not support such notation, so someone uses the split method to achieve it.

0
source

If a large number of arrays are built manually. This can potentially shorten the loading time of your page as fewer characters are transmitted. But you will need a large number of arrays or large arrays to have a significant difference. Array creation performance may be slower, but faster to enter. For large arrays, I use a spreadsheet to apply formatting around each value using the following formula: "& A1 &" ",". I am sticking with an array literal.

0
source

It makes sense to use the "split" method when you cannot control the output, i.e. user input or output of a string from a method. If you are trying to get a specific value in the output that is shared by something, it is easier to use the split method. Of course, if you control values, this does not always make sense.

0
source

Source: https://habr.com/ru/post/981585/


All Articles