user2662692
user2662692

Reputation: 237

Where should I store static data in a web application

I have a general question about writing web applications. Many times I have static data, say a list of countries, or a list of options for radio buttons or a drop down menu. I don't like hard coding this stuff into the HTML because it makes it hard to change. I am wondering what is the best place to store the data. I've been storing it in tables in a database, but would rather not do this as I want to avoid slowing my application down by making unnecessary database calls. Should I just load it in the actual code file such as:

countries = ["America", "Canada", .... ]

or is there a better place?

Does it matter how much data it is? Would the answer change from a list of 5 items to a list of 500 or 5,000.

Upvotes: 5

Views: 2316

Answers (2)

Gren
Gren

Reputation: 1854

The database is the right place, because some of this data is not so static as you might think at the beginning of a project. Additional, but not alternatively, you can use config files.
You can create a cache for this static or seldom changed data to improve the performance.

Upvotes: 1

user3542327
user3542327

Reputation:

It depends on what you want.

Hardcoding into the html is faster, and more efficient when your list is small. However, as your list grows longer, a database could be a better choice, because modifying the database is easier.

An alternative option is to store the list in a separate file, and load it into your html using ajax, which is lighter than a database, and easier to modify than hardcoding.

Upvotes: 3

Related Questions