I am creating a Node.js application that will store a huge amount of data, so I want to plan ahead and think about how I should structure the data.
Let's say I want to save 500,000 student accounts:
ID: unique string,
username: string,
password: string,
Name: string,
Age: int,
grade: string,
Now, what is the best, fastest, and most effective way to structure data to get specific student account information? Example. If a student wants to log in, we must verify their credentials.
Therefore, if we save the information as an array of students, we will have to sort through the array. Will it slow down the application if we have a huge number of people trying to log in at the same time?
I came up with two different ways to do this, but I don’t know which one is faster and more efficient. Please explain this in your answers.
1. The first method
Store them as JSON objects, and the key object will be a unique identifier - in this case it will be the student identifier. Example:
var database = {};
database["SID0001"] = {
"ID": "SID0001",
"username": "moe-kanan",
"password": "123123",
"name": "Moe Kanan",
"age": 99,
"grade": "A"
}
in this method, I do not need to quote. I can get the credentials simply by doing this:
var username = database["SID0001"].username;
var password = database["SID0001"].password;
2. The second method
var database = [];
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];
return {"username": obj.username, "password": obj.password}
}
getStudentInfo("SID0001");
Please feel free to add better solutions :) I really appreciate it!
NOTE. Keep in mind that I do not want to use the database, but I will use MongoDB in the future.