I'm building a Node.js app that will store a huge amount of data, so I want to plan ahead and think of how I should structure the data.
Let's say I want to save 500,000 student accounts information:
ID: unique string, // SID0001
username: string, // moe_kanan
password: string, // 123123
Name: string, // Moe kanan
Age: int, // 1 to 100
grade: string, // A, B, C or D
Now, what is the best, fastest and most efficient way to structure the data to get a specific student's account information? Example, If a student wants to login, we have to check their credentials.
Therefore, If we save the information as an array of students, we will have to loop through the array. Will this slow the app if we have a huge amount of people tries to login at the same time?
I came up with two different ways to do it, but I don't know which one is faster and more efficient. Please explain that in your answers.
1. First method
Store them as JSON objects, and the object key will be the unique id - in this case would be the student ID. Example:
var database = {}; //NOTICE this is an object
database["SID0001"] = {
"ID": "SID0001",
"username": "moe_kanan",
"password": "123123",
"name": "Moe Kanan",
"age": 99,
"grade": "A"
}
in this method, I don't have to loop. I can get the credentials just by doing this:
var username = database["SID0001"].username; //moe-kanan
var password = database["SID0001"].password; //123123
2. Second method
var database = []; //NOTICE this is an array
database.push({
"ID": "SID0001",
"username": "moe_kanan",
"password": "123123",
"name": "Moe Kanan",
"age": 99,
"grade": "A"
});
var getStudentInfo = (id) => {
let obj = database.filter(student => student.ID == id)[0]; //NOTICE the [0]
return {"username": obj.username, "password": obj.password}
}
getStudentInfo("SID0001"); //{username: "moe_kanan", password: "123123"}
Please feel free to add better solutions :) I really appreciate it!
NOTE: Keep in mind, I don't want to use a database for now, but I will use MongoDB in the future.