List Operations
JS
S
JavaScriptSimple operations using yallist to manage the iterable map.
1const tree = require('./tree.json');
2var yallist = require('yallist')
3var taxonomyTree = yallist.create(tree);
4
5// Unordered List (<1000ms)
6const categoryComputers = "/Computers & Electronics/Software";
7console.time('telemetry');
8console.log('head', taxonomyTree.head);
9console.log('tail', taxonomyTree.tail);
10
11// Stage 1: Find nodes with the full category
12const matchNodes1Tree = yallist.create(taxonomyTree.reduce(function (set, entry) {
13 if (entry.externalCategories) {
14 const externalCategories = entry.externalCategories[0];
15 if(externalCategories && externalCategories.keywords) {
16 const exists = externalCategories.keywords.find(keywordsItem => keywordsItem === categoryComputers);
17 if(exists){
18 set.push(entry);
19 }
20 }
21 }
22 return set
23}, []));
24
25console.log('matchNodes1Tree', matchNodes1Tree.length)
26// console.log('matchNodes1Tree', matchNodes1Tree)
27
28// Stage 2 (Build Children Tree)
29const childrenOfNodes2Tree = yallist.create(matchNodes1Tree.reduce(function (set, entry) {
30 const { _id } = entry; // id
31 taxonomyTree.forEach(function (k) {
32 const { parent } = k;
33 if (parent === _id) {
34 set.push(k);
35 }
36 });
37 return set
38}, []));
39
40console.log('childrenOfNodes2Tree', childrenOfNodes2Tree.length)
41
42
43// Stage 3 (Build Children Tree)
44const childrenOfNodes3Tree = yallist.create(childrenOfNodes2Tree.reduce(function (set, entry) {
45 const { _id } = entry; // id
46 taxonomyTree.forEach(function (k) {
47 const { parent } = k;
48 if (parent === _id) {
49 set.push(k);
50 }
51 });
52 return set
53}, []));
54console.log('childrenOfNodes3Tree', childrenOfNodes3Tree.length)
55
56
57// Stage 4 (Build Children Tree)
58const childrenOfNodes4Tree = yallist.create(childrenOfNodes3Tree.reduce(function (set, entry) {
59 const { _id } = entry; // id
60 taxonomyTree.forEach(function (k) {
61 const { parent } = k;
62 if (parent === _id) {
63 set.push(k);
64 }
65 });
66 return set
67}, []));
68console.log('childrenOfNodes4Tree', childrenOfNodes4Tree.length)
69console.log('taxonomyTree', taxonomyTree.length)
70console.timeEnd('telemetry');
71/*
72 childrenOfNodes2Tree 142
73 childrenOfNodes3Tree 764
74 childrenOfNodes4Tree 0
75 taxonomyTree 5328
76 telemetry: 195.973ms
77*/
78
79
80
81
82// Stage 2: Get all children of nodes which match category
83// const childrenOfNodes2Tree = yallist.create(matchNodes1Tree.reduce(function (set, entry) {
84// const { children } = entry;
85// console.log('iterating..', children)
86// children.forEach(function (child1) {
87// set.push(child1);
88// const { children } = child1;
89// children.forEach(function (child2) {
90// set.push(child2);
91// const { children } = child2;
92// children.forEach(function (child3) {
93// set.push(child3);
94// const { children } = child3;
95// children.forEach(function (child4) {
96// set.push(child4);
97// });
98// });
99// });
100// });
101// }, []));
102
103// console.log('childrenOfNodes2Tree', childrenOfNodes2Tree.length)Created on 3/9/2021