JavaScript / Node.js Best Practice for Storing JSON Objects And The Most Effective Way to Get Certain Values

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,   // 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 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 = {}; //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 do not need to quote. I can get the credentials simply by doing this:

var username = database["SID0001"].username;  //moe-kanan
var password = database["SID0001"].password;  //123123

2. The 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 that I do not want to use the database, but I will use MongoDB in the future.

+4
source share
3 answers

, , /, .

O(1) - O(n) .

, , .

+3

, , , JSON . O(1), .

file per row, .

, , , , :

0001 :

/storage-directory/0/0/0/0001.json

, ID. , , , , . , .

(, SHA1) , .

SHA1(0000001) 82c27eaf3472b30a873d39f4342f5e54de9532b9

:

/storage-directory/8/2/c/0000001.json

getStudentInfo :

this.getStudentInfo = (id) => {
    let index = this.sha1Index(id);
    let key = index[0]+"/"+index[1]+"/"+index[2]+"/"+id+".json";

    return fs.parseJson(this.storageDirectory+"/"+key);
}

, , SHA1 let say 1 001, (, 0).

, .

+1

, , , "MySQL". , - , Node.js? SQL , 500 000? , SQL - .

0
source

Source: https://habr.com/ru/post/1688492/


All Articles