2

I frequently see code where people define a populated array using the split method, like this:

var colors = "red,green,blue".split(',');

How does this differ from:

var colors = ["red","green","blue"];

Is it simply to avoid having to quote each value?

5
  • 10
    the first is just a bad habit. Commented Nov 11, 2016 at 22:03
  • 1
    And to spend more CPU cycles. Commented Nov 11, 2016 at 22:03
  • I think maybe someone want to have special code :). Splitting to create array has performance issues, it need to do operation on string before. Commented Nov 11, 2016 at 22:05
  • Well 1st method is creating an overhead of splitting a string to an array and then adding It to the variable. 2nd method is a good approach. Although you can also use var colors = new Array(); and then push() data into it. Commented Nov 11, 2016 at 22:05
  • 2
    The first has fewer presses of the Shift key :). Commented Nov 11, 2016 at 22:09

2 Answers 2

3

Splitting a string is a bad way of creating an array. There several issues with the approach that include performance, stability and memory consumption. It requires CPU time to parse the string, it is prone to errors (double commas, spaces in the string, etc.) and means your script essentially has to store twice as much data in memory.

It's not a good idea and is most likely just a bad habit someone picked up when they first learned about strings and arrays. That or they're trying to be clever for some kind of coding exercise.

As a rule of thumb, the only time you should be parsing strings into arrays is if you're reading that string data from an external source and need to convert it to native types. If you already know the values ahead of time, you should create the array yourself.

The one possible reason someone might do this is to reduce the number of characters in their source code, trading performance for bandwidth. 'a,b,c,d,e,f,g'.split(',') is fewer characters than ['a','b','c','d','e','f','g'].

Sign up to request clarification or add additional context in comments.

7 Comments

Correct me if I'm wrong(and I very well may be) but there is an outlier use case. If you're looking to turn a string into a character array "abcdefgh".split('') works well for gaining array methods quickly.
@zfrisch well you could do that but I can't think of why you'd have a really long string you have to hardcode and then split. And I'm talking about a long string because if it's short, you may as well just do the array - sure, it's slightly annoying to type but if it's really a-h, I'd just type it by hand.
I added one possible outlier reason; shorter source code if there were a lot of items in that list.
Thanks. It seemed like it had to be less efficient but I've seen it enough that I had to ask.
@vlaz You're right, I kept thinking about this as being an inputted string like in the standard palindrome problem jsfiddle.net/f15r8424 , but with hardcoding there's really not a reason to outside of shorter code.
|
1

There is no difference, it's just bad practice and laziness if anything. The only reason I could think of using the first approach is if the data naturally came in string form and using an array literal made it completely unreadable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.