Skip to content

Commit 36eba26

Browse files
committed
formulas: sanity/ removed invalid operations
1 parent 621465d commit 36eba26

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

formulas.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ function reducePerformance(posts) {
2121
console.log('*************** Reduce performace check ***************')
2222
const length = posts.length;
2323
let avg = 0;
24+
2425
console.time('js reduce');
25-
avg = posts.reduce((avg, p) => avg+= (+p.downvotes+ +p.upvotes+ +p.commentCount)/3,0);
26+
avg = posts.reduce((acc, p) => acc+= (+p.downvotes+ +p.upvotes+ +p.commentCount)/3,0);
2627
avg = avg/length;
2728
console.timeEnd('js reduce')
2829

@@ -44,7 +45,7 @@ function reducePerformance(posts) {
4445

4546
avg = 0;
4647
console.time('lodash reduce');
47-
avg = reduce(posts, p => avg+= (+p.downvotes+ +p.upvotes+ +p.commentCount)/3,0);
48+
avg = reduce(posts, (acc, p) => acc+= (+p.downvotes+ +p.upvotes+ +p.commentCount)/3,0);
4849
avg = avg/length;
4950
console.timeEnd('lodash reduce');
5051
}
@@ -54,30 +55,52 @@ function mapPerformance(posts) {
5455
console.log('*************** Map performace check ***************')
5556
const divider = random(1,300);
5657
const length = posts.length;
58+
let newData = [];
59+
5760
console.time('js map');
58-
posts.map(p => {
59-
p.upvotes = (p.upvotes+p.commentCount)/divider;
60-
return p;
61+
newData = posts.map(p => {
62+
return {
63+
id: p.id,
64+
upvotes: (+p.upvotes + +p.commentCount)/divider,
65+
downvotes: p.downvotes,
66+
commentCount: p.commentCount
67+
};
6168
});
6269
console.timeEnd('js map')
63-
70+
71+
newData=[];
6472
console.time('for loop');
6573
for(i=0; i<length; i++) {
66-
posts[i].upvotes = (+posts[i].upvotes + +posts[i].commentCount)/divider;
74+
newData.push({
75+
id: posts[i].id,
76+
upvotes: (+posts[i].upvotes + +posts[i].commentCount)/divider,
77+
downvotes: posts[i].downvotes,
78+
commentCount: posts[i].commentCount
79+
});
6780
}
6881
console.timeEnd('for loop');
6982

83+
newData=[];
7084
console.time('for each');
7185
posts.forEach(element => {
72-
element.upvotes = (+element.upvotes + +element.commentCount)/divider;
86+
newData.push({
87+
id: element.id,
88+
upvotes: (+element.upvotes + +element.commentCount)/divider,
89+
downvotes: element.downvotes,
90+
commentCount: element.commentCount
91+
});
7392
});
7493
console.timeEnd('for each');
75-
76-
avg = 0;
94+
95+
newData=[];
7796
console.time('lodash map');
78-
avg = map(posts, p => {
79-
p.upvotes = (p.upvotes+p.commentCount)/divider;
80-
return p;
97+
newData = map(posts, p => {
98+
return {
99+
id: p.id,
100+
upvotes: (+p.upvotes + +p.commentCount)/divider,
101+
downvotes: p.downvotes,
102+
commentCount: p.commentCount
103+
};
81104
})
82105
console.timeEnd('lodash map');
83106
}
@@ -87,14 +110,14 @@ function mapPerformance(posts) {
87110
// commentCounts*0.1) multiple by a weight and return -> filter
88111
function filterPerformance(posts) {
89112
console.log('*************** Filter performace check ***************')
90-
const fitlerValue = random(1,200);
113+
const fitlerValue = random(1,50);
91114
const length = posts.length;
92115
let newData = [];
93116

94117
console.time('js filter');
95118
newData = posts.filter(p => (+p.upvotes*0.2 + +p.downvotes*0.3 +p.commentCount*0.1)/3 > fitlerValue);
96119
console.timeEnd('js filter')
97-
120+
98121
newData = [];
99122
console.time('for loop');
100123
for(i=0; i<length; i++) {
@@ -115,24 +138,26 @@ function filterPerformance(posts) {
115138

116139
newData = [];
117140
console.time('lodash filter');
118-
avg = filter(posts, p => (+p.upvotes*0.2 + +p.downvotes*0.3 +p.commentCount*0.1)/3 > fitlerValue);
141+
newData = filter(posts, p => (+p.upvotes*0.2 + +p.downvotes*0.3 +p.commentCount*0.1)/3 > fitlerValue);
119142
console.timeEnd('lodash filter');
120143
}
121144

122145

123146
// find the last post
124147
function findPerformance(posts) {
125148
console.log('**************** Find performace check ***************')
126-
let obj = {};
149+
const randomFind = random(0, posts.length-1);
127150
const length = posts.length;
151+
152+
let obj = {};
128153
console.time('js find');
129-
obj = posts.find(p => p.id == length);
154+
obj = posts.find(p => p.id == randomFind);
130155
console.timeEnd('js find');
131156

132157
obj = {};
133158
console.time('for');
134159
for(i=0; i<length; i++) {
135-
if(posts[i].id == length) {
160+
if(posts[i].id == randomFind) {
136161
obj = posts[i];
137162
}
138163
}
@@ -141,14 +166,15 @@ function findPerformance(posts) {
141166
obj = {};
142167
console.time('for each');
143168
posts.forEach(element => {
144-
if(element.id === length) {
169+
if(element.id == randomFind) {
145170
obj = element
146171
}
147172
});
148173
console.timeEnd('for each');
149174

150175
obj = {};
151176
console.time('lodash find');
152-
find(posts, p => p.id === length)
177+
obj = find(posts, p => p.id === randomFind)
153178
console.timeEnd('lodash find');
179+
154180
}

generate_data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const max = process.argv[2] ? process.argv[2] : 100000;
99
const posts = range(0, Number(max)).map((p, i) => {
1010
return {
1111
id: i,
12-
votes: p+random(0, 500),
12+
upvotes: p+random(0, 500),
1313
downvotes: p+random(0, 500),
1414
commentCount: random(0, 1000)
1515
}

0 commit comments

Comments
 (0)