-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
247 lines (231 loc) · 7.28 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* Dependencies
*/
var table = require('text-table');
var now = require("performance-now");
/**
* Main export
*/
module.exports = function (config)
{
if (!config.counts) throw new Error('Looper requires loop counts');
if (!config.repeat) config.repeat = 10;
/**
* Build al lthe necessary data & setup all holder variables
*/
var someFn = config.work || function () {};
var results = {};
var loopData = [];
console.log('Building data...');
config.counts.forEach(function (count)
{
var currData = [];
while (currData.length < count)
{
currData.push(
{
"_id": "58fce0362c21d0235773c075",
"index": 0,
"guid": "4956df30-4faf-4cc1-be06-9dead4cb336d",
"isActive": false,
"balance": "$3,529.54",
"picture": "http://placehold.it/32x32",
"age": 22,
"eyeColor": "brown",
"name": "Green Love",
"gender": "male",
"company": "INFOTRIPS",
"email": "[email protected]",
"phone": "+1 (833) 437-2858",
"address": "450 Halsey Street, Cataract, Minnesota, 2472",
"about": "Do nulla ipsum ut velit tempor ea et labore aute adipisicing consequat aliqua officia in. Voluptate reprehenderit velit veniam proident aliquip aliqua occaecat duis nulla. Irure sint occaecat ullamco dolore aute magna est sit. Id dolore proident pariatur aliqua ullamco excepteur esse consequat irure occaecat enim dolore proident labore. In proident minim magna mollit aliquip ipsum occaecat mollit magna occaecat occaecat sint ullamco dolore. Magna nisi excepteur dolor laboris esse ea ipsum et quis.\r\n",
"registered": "2014-03-18T03:03:08 +05:00",
"latitude": 63.373698,
"longitude": 123.784373,
"tags": [
"ullamco",
"id",
"aliquip",
"duis",
"minim",
"tempor",
"elit"
],
"friends": [
{
"id": 0,
"name": "Fern Middleton"
},
{
"id": 1,
"name": "Wilkinson Graves"
},
{
"id": 2,
"name": "Melba Wells"
}],
"greeting": "Hello, Green Love! You have 4 unread messages.",
"favoriteFruit": "banana"
});
}
loopData.push(currData);
});
/**
* Use text-table to log the results in a table format
*/
var prettyLog = function ()
{
var tableData = [
['LOOP TYPE', ...config.counts]
];
var bases = [];
Object.keys(results['while desc']).forEach(function (countType)
{
var times = results['while desc'][countType];
var sum = times.reduce(function (acc, val)
{
return acc + val;
}, 0);
bases.push(sum);
});
Object.keys(results).forEach(function (loopType)
{
var currentResult = [loopType];
Object.keys(results[loopType]).forEach(function (countType, idx)
{
var times = results[loopType][countType];
var sum = times.reduce(function (acc, val)
{
return acc + val;
}, 0);
var relative = '-';
if (bases[idx] > sum) relative = '-' + String((bases[idx] / sum).toFixed(2)) + '%';
else if (bases[idx] < sum) relative = '+' + String((bases[idx] / sum).toFixed(2)) + '%';
currentResult.push(relative);
});
tableData.push(currentResult);
});
console.log('\n\n' + table(tableData) + '\n');
process.exit();
}
/**
* Base loops to compare your own loops against
*/
var methods = {
'for loop - length uncached': function (data, func)
{
for (var x = 0; x < data.length; x++)
{
func(data[x]);
}
},
'for loop - cached length': function (data, func)
{
var l = data.length;
for (var x = 0; x < l; x++)
{
func(data[x]);
}
},
'for loop - i--': function (data, func)
{
var i = data.length - 1;
for (; i > 0; i--)
{
func(data[i]);
}
},
'for in': function (data, func)
{
for (var i in data)
{
func(data[i]);
}
},
'forEach': function (data, func)
{
data.forEach(func);
},
'while': function (data, func)
{
var i = 0;
while (i < data.length)
{
func(data[i++]);
}
},
'do while': function (data, func)
{
var i = 0;
do
func(data[i++]);
while (i < data.length);
},
'while desc': function (data, func)
{
var i = data.length;
while (i--)
{
func(i);
}
},
'do while desc': function (data, func)
{
var i = data.length;
do
func(i);
while (i--);
},
'pop': function (data, func)
{
while (data.length)
{
func(data.pop());
}
}
};
/**
* Add the loops passed to the list
*/
Object.assign(methods, config.loops);
/**
* Setup results holder object
*/
Object.keys(methods).forEach(function (loopType)
{
results[loopType] = {};
config.counts.forEach(function (count)
{
results[loopType][count] = [];
});
});
/**
* Run all loops in a reduce chain to avoid caching issues
*/
var repeatArray = Array(config.repeat).fill().map((_, i) => i * i);
console.log('Looping...');
return repeatArray.reduce(function (lastIter, currIter, idx)
{
return lastIter.then(function ()
{
process.stdout.write(idx ? idx + ' ' : 'Iteration: ' + idx + ' ');
return Object.keys(methods).reduce(function (prevLoop, currLoop)
{
return prevLoop.then(function ()
{
return loopData.reduce(function (lastCounts, currCounts)
{
return lastCounts.then(function ()
{
var copy = [...currCounts];
var t0 = now();
methods[currLoop](copy, someFn);
var t1 = now();
results[currLoop][currCounts.length].push(t1 - t0);
});
}, Promise.resolve());
});
}, Promise.resolve());
});
}, Promise.resolve()).then(prettyLog);
};