Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: JS array implementation?

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default JS array implementation?

    Does anyone know how js arrays are implemented? Are they like maps, linked lists, java arrays, or what? Or perhaps different browsers implement them in different styles?

    My guess is that they are map-like because of the ability to dynamically add properties to objects. But still, I would like to know how it works.

    Also, let's say you are making an array of length 10. Based on its implementation, would
    Code:
    var a = new Array(10);
    for(var i = 0; i < 10; ++i)
      a[i] = i;
    make any performance difference over the following?
    Code:
    var a = [];
    for(var i = 0; i < 10; ++i)
      a[i] = i;
    [].shift() vs [].pop() speeds also come to mind.
    Last edited by Trinithis; 12-17-2007 at 05:56 AM.
    Trinithis

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Arrays are just objects with some setters and getters for numerical properties. The number you use in the square brackets is converted to a string; if 1 were a legal identifier, you'd be able to do [1, 2, 3].1.
    Based on its implementation, would [pre-declaring the array's length] make any performance difference ...?
    Depends on the implementation. If I remember correctly, in Spidermonkey no: it just sets the length. It wouldn't be unthinkable that the implementation could create the appropriate properties as well, though.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The two declaration methods are equivalent. There might be minor speed issues. Only with huge arrays could these amount to any appreciable matter.

    I think pop() has been around longer and isn't as versatile.

    Something you alluded to, but didn't come right out and say is that arrays may have properties assigned to them, ex:

    Code:
    var a = new Array(10);
    for(var i = 0; i < 10; ++i)
      a[i] = i;
    a.someProp=document.images[0];
    a.anotherProp='orange';
    Which I've often made extensive use of, as it can come in quite handy at times.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Something you alluded to, but didn't come right out and say is that arrays may have properties assigned to them
    Any object can have properties assigned to it. Generally you'd be better off using an Object or a user-defined Hash object, since ECMAScript arrays are really designed for numerical keys, and adding properties might break some things (for..in for instance).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Thanks for the replies. I mainly just wanted to make sure that shifting an array wouldn't cause all the overhead that non-associative arrays have to deal with. Same for other methods that could be time-consuming unless handled properly.

    Edit:
    Whoops! Nevermind. Shifting and unshifting do come at a performance cost. Still fast if the array length is less than 1000.


    As for hashes, I've seen stuff like an array/ordinary object that keeps track of the keys along with indices so it can be iterated w/o a for-in loop. It's always fun to see how people do things in different ways though for learning experiences.
    Last edited by Trinithis; 12-17-2007 at 07:14 AM.
    Trinithis

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I do like the way you code . . . Hopefully I'm getting there . . .

    You got any other library-type code I could study?
    Last edited by Trinithis; 12-17-2007 at 07:24 AM.
    Trinithis

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops! Nevermind. Shifting and unshifting do come at a performance cost.
    Yes they do, since all the elements of the array must be rearranged. This would be more efficient were the array implemented as a linked list, but at the cost of performance elsewhere.
    I do like the way you code
    Haha, I'm glad
    You got any other library-type code I could study?
    Probably. On what topic?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Anything you've got. One could always learn more. If you want you can PM it to me. Or you could post it. Anyway, until later. Sleep beckons.
    Trinithis

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's likely all buried deep in the DD archives, my hard drive, and/or my brain. I'd need keywords to search it out from any of those sources.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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