What is the most convenient way to declare array in JS?

2

I've been observing a lot of people declaring arrays as follows

monthShort : 'Jan_Fev_Mar_Abr_Mai_Jun_Jul_Ago_Set_Out_Nov_Dez'.split('_')

But I've also observed using it like this:

monthShort : ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']

So, is there any performance difference? If so, which one? Thank you :)

    
asked by anonymous 08.03.2016 / 19:54

1 answer

4

I suggest that in this type of question you take into account that:

  • Sometimes these types of tests give different results in different browsers
  • It is important to write code with correct semantics. If the code fools what you do, the most semantically correct way is preferable.
  • Differences in performance are only relevant in cases where there are thousands of accounts about the same process. Most of the time you can not tell the difference.

In the tests I did (Chrome on jsFiddle and jsPerf ) the fastest way is the most semantic way. In jsPerf the difference is almost irrelevant.

monthShort : ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']
    
08.03.2016 / 20:09