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

Asked

Viewed 33 times

2

I have been watching a lot people declare arrays the following way

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

But I’ve also seen them use 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? Grateful :)

1 answer

4


I suggest that in such matters it should be borne in mind that:

  • Sometimes these kinds of tests give different results in different browsers
  • It is important to write code with correct semantics. If the code is wrong about what it does, it is preferable to use the most correct semantic way.
  • Performance differences are only relevant in cases where there are thousands of accounts on the same process. Most of the time you can’t see the difference.

In the tests I did (Chrome no 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']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.