DreamWave
DreamWave

Reputation: 1940

How many variables can a javascript array hold?

I have a javascript array that is dynamically populated. Example: Array name $testarr[] And I have a database that fills it with numbers like this $testarr[id]=number. As my database is constantly growing I need to know when the array will hit the roof and refuse to take any more variables

Upvotes: 2

Views: 3258

Answers (3)

jondavidjohn
jondavidjohn

Reputation: 62392

You're not dealing with a limitation of the Array javascript object, you're dealing with the memory limitation of the browser/machine combo that is running your scripts.

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

Apparently it is unlimited only by system memory, although the length property fails if indices get larger than 1073741822 according to this source of 1997.

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258618

This is browser-specific, so you need to check with the browser documentation. There have actually been exploits that took advantage of this, as a large enough array would go beyond the maximum allowed to by the browser.

I believe your design is faulty, as if you need to hold this many variables in javascript, you should probably re-think your design.

EDIT: apparently there is a limit - 2^32 - 1 - but I believe you'll reach the limit imposed by the browser before you get to 2^32 - 1 elements in the array.

Upvotes: 3

Related Questions