-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (52 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
console.log(users);
// STEP 1 - find object without indexing
/*
function findDocumentByIdWithoutIndex(collection, id, city) {
for (let i = 0; i < collection.length; i++) {
if (collection[i].id === id && collection[i].city === city) {
return collection[i];
}
}
}
console.time("Find user without index");
var foundUser = findDocumentByIdWithoutIndex(users, 39999, "Los Angeles");
console.timeEnd("Find user without index");
console.log(foundUser);
*/
//
// STEP 2 - Index the users by city name
/*
function createIndex(data) {
const index = {};
data.forEach(function(userData) {
if (!index[userData.city]) {
index[userData.city] = [userData.id];
} else {
index[userData.city].push(userData.id);
}
});
return index;
}
console.time("Index the users by city");
const indexedUsers = createIndex(users);
console.timeEnd("Index the users by city");
// INDEXED USERS
console.log(indexedUsers);
*/
//
// STEP 3 - Find documents indexed by city
/*
function findIndexedDocument(id, city) {
for (let i = 0; i < indexedUsers[city].length; i++) {
if (
indexedUsers[city][i].id === id &&
indexedUsers[city][i].city === city
) {
return indexedUsers[city][i];
}
}
}
console.time("Find indexed user");
var foundUser2 = findDocumentByIdWithoutIndex(39999, "Los Angeles");
console.timeEnd("Find indexed user");
*/