views:

168

answers:

3

Is it advisable to use arrays in Javascript using tens of thousands of indexes?

I have a database which contains a table of static data, meaning that it will never change. In this case would it be advisable to convert this table to a javascript array and look the info up in there when needed? Is this bad practice?

The table in question contains roughly 40,000 key/value pair entries.

+6  A: 

No.

I'd keep the table, as its a single point of maintenance.

You'll find that doing a XmlHTTPRequest to return a key/value pair based on a server side query, would actually perform faster, and have significantly less memory footprint than a huge JavaScript array.

FlySwat
+2  A: 

In my experience, moving beyond a few thousand lines in an array cached client-side in a browser led to hellish experiences. Bloated browser memory footprints, slow load times and general sluggishness were the order of the day. You should test this scenario yourself for the experience. It's worth the few minutes it'd take you.

x0n
+1  A: 

The main thing to take into consideration is end-user performance. Assuming that it works well on your machine will not mean it works well on an older machine. The riskiest part about the client-side aspect of your approach is that it depends heavily on what the client has. Personally, I would avoid putting that much data to the client, but I don't know enough of the background to your project to determine why you might need to do this.

Some sort of AJAX approach retrieving cached data from the server might be more appropriate.

joseph.ferris