Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post

    Creating an Array in the most efficient way

    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:
    Code:
      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:
    Code:
      arr=new Array("value 0","value 1","value 2","value 3","value 4");
    Literal Notation:
    Code:
      arr=["value 0","value 1","value 2","value 3","value 4"];
    String Split:
    Code:
      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?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by JohnKrutsch
    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.
    http://lxr.mozilla.org/seamonkey/source/js/src/jsstr.c

    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?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •