View Full Version : Creating an Array in the most efficient way

08-18-2004, 05:05 AM
I did a quick goggle and search of the forums but came up empty. I am wondering what the most efficient way of creating an array is, and alternatively what are the pros and cons of each method. Consider these:

Conventional array:

arr=new Array();

arr[0]="value 0";
arr[1]="value 1";
arr[2]="value 2";
arr[3]="value 3"
arr[4]="value 4"

Dense array:

arr=new Array("value 0","value 1","value 2","value 3","value 4");

Literal Notation:

arr=["value 0","value 1","value 2","value 3","value 4"];

String Split:

arr="value 0,value 1,value 2,value 3,value 4".split(",");

As far as physically coding it, I like the String Split method but what price am I paying in Processing overhead? IS there a better way? A more efficient way, or is one just as good as another?

Given my druthers I would use string split unless I am taking a bit hit on performance then I would prefer to use literal notation.

How about you?

08-18-2004, 05:20 AM
Your first method does not assign a length. And since a Javascript Array is more like a vector data object, by exceeding the length of the array (currently zero) on each assignment, you are forcing a resize. Maybe internally it initializes enough memory to not be an issue. However, if it doesn't, then you will copy the entire array over in memory a few times unnecessarily. So I recommend against it, unless you provide a length to the constructor.

2 and 3 are probably the best.

Your string split requires an O(n) scan through the string, then the extraction of the substrings. That will cost some performance.

In small amounts of data, like we are dealing with, none of the methods will probably make any difference. So efficiency doesn't matter... only in massive amounts will you see the actual performance hits.

08-18-2004, 06:14 AM
Yeah it is massive amounts I am concerned about these are just examples to get the point across.

Do you know of any resources where I can read about the scan and extraction that the split method uses? I suspected that there was significant overhead associated with that method but want to know more about it.

So it looks as if the Literal Notation is the most efficient way to go.

Anyone else want to chime in?

08-18-2004, 06:59 AM
Do you know of any resources where I can read about the scan and extraction that the split method uses? I suspected that there was significant overhead associated with that method but want to know more about it.


Knock yourself out. It just looks like an iteration through the characters, then returning appropriate substrings.

I think some benchmarking would tell you for sure though? :)

08-18-2004, 07:23 PM
In case anyone is interested here are the results of some benchmarking I did:

I loaded the same 10,000 items into each of the different array styles to test their efficiency in creating arrays. I did this 100 times for each array type. On average (mean):
It took 0.15666000000000005 seconds to load the Conventional Array.
It took 0.04040999999999999 seconds to load the Dense Array.
It took 0.04147999999999998 seconds to load the Literal Array.
It took 0.07140000000000002 seconds to load the Split Array.

With speeds this fast is performance really an issue for any of the methods?

08-19-2004, 05:43 AM
Logically, I prefer the fastest method even if the difference is minimal. Then depending on the situation, I would consider the other methods. The Split method is useful if you have a dynamic string that need to be turned into an array. Naturally, you would use the split method in that case. So mostly it's a choice between Dense and Split arrays.

08-19-2004, 06:57 AM
if you ask me the string method is a disaster :)

1:would be hard making it hold objects.
2:any numbers that are stored in it would have to be converted to numbers after a split just as a safety measure.
3:it takes up a char/chars to use as seperator, meaning this char/chars can't be used in the data.

I personaly prefer the 1st array notation cause I find it alot more readable than 2 and 3, which also mean the download file gets bigger(you have to type arrayName[index] all the time).

08-19-2004, 11:10 AM
wouldn't the fastest array methods come into their own when dealing with large datasets of multi-dim arrays?

with that in mind, i prefer literal notation. it's easy to read, shorter to type and fast to process.