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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array max length?

    What is the longest possible array?

    new Array(Number.MAX_VALUE) // ERROR.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DHTML Kitchen
    What is the longest possible array?

    new Array(Number.MAX_VALUE) // ERROR.
    I did a little searching but could not find a reference that stated what the max size number of elements could be. So I did a little testing on my own. I used a web page from www.w3schools.com

    Code:
     
    <html>
    <body>
    <script type="text/javascript">
    var arr = new Array(3)
    arr[0] = "John"
    arr[1] = "Andy"
    arr[2] = "Wendy"
    document.write("Original length: " + arr.length)
    document.write("<br />")
    arr.length=4294967295
    document.write("New length: " + arr.length)
    </script>
    </body>
    </html>
    If I use a number any higher than the one in bright blue in the above code (4,294,967,295) I would get the following error code in FoxFire:

    Error: invalid array length
    Source File: http://www.w3schools.com/jsref/tryit_view.asp
    Line: 14


    I tried it on 3 different computers with different amounts of RAM, with the same results.

    But I don't think it would be a good ideal to use an array that size.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Even if each entry in an array that big were only one byte in size it would take w-------a--------y too long for the web page to download to be usable. If each entry were a lot larger it could easily exceed the entire space on your web hosting.

    The practical limit is probably in the hundreds if not lower.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JS doesnt put a limit on the size of an array. The only restraint is that of the user's computer. No matter the amount of memory/processor speed that a computer has it will still have a set limit, and for most the # will be close to that which you found.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "konithomimo",

    I think I found the reason for the limit of 4,294,967,295 elements in a javascript array. That is the max size of an interger (-2,147,483,648 to 2,147,483,648)

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Javascript currently only has one number type, floating-point, which is encapsulated as the object type "Number". Evidently, the actual value is held in binary as an IEEE Standard 754 Double. The range therefore slightly exceeds &#177;1.7&#215;10308. Integers can only be held accurately up to, I think, 53 bits plus sign, and numbers are only, at best, accurate to 15-16 significant decimal digits. Note, however, that any integer from 0 up to 253 = 9,007,199,254,740,992 in magnitude should be stored exactly, but relatively few decimal fractions can be. Only the numbers obtainable from one of those integers by multiplying by 2N-53 for any N in -1023 to +1023 can be held exactly. Check in detail, though.
    That should explain it to you. Basically, unlike other programming languages JS only has one type of number. Other languages usually have more than one (integer,double,etc.)


  •  

    Posting Permissions

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