This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
collection_each_example.js
254 lines (217 loc) · 4.7 KB
/
collection_each_example.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
248
249
250
251
252
253
254
'use strict';
var _ = require('underscore');
var async = require('async');
var stormpath = require('./lib');
var moment = require('moment');
var homeDir = process.env[(process.platform === 'win32' ? 'USERPROFILE' : 'HOME')];
var apiKeyFilePath = homeDir + '/.stormpath/apiKey.properties';
var client = null;
var itemsPerPage = 100;
stormpath.loadApiKey(apiKeyFilePath, function (err, apiKey) {
if (err) {
throw err;
}
client = new stormpath.Client({apiKey: apiKey});
client.getCurrentTenant(function (err, tenant) {
if (err) {
throw err;
}
//add 200 applications for testing
// for(var i = 0; i < 200; i++){
// tenant.createApplication({name: 'Just Testing! Delete me.' + Math.random() },
// function(){})
// }
tenant.getApplications({limit: itemsPerPage}, function (err, apps) {
if (err) {
throw err;
}
onAppsReady(apps);
});
});
});
function onAppsReady(apps) {
function s(f) {
return function (cb) {
console.log('Testing ' + f.name);
f(apps, cb);
};
}
async.series([
s(each),
s(map),
s(filter),
s(reject),
s(reduce),
s(detect),
s(some),
s(every),
s(sortBy),
s(concat)
], function (err) {
if(err){
console.error(err);
}
console.log('all done');
});
}
function Logger() {
var self = this;
var start = Date.now();
var counter = 0;
var hundred = Date.now();
this.reset = function reset() {
start = Date.now();
counter = 0;
hundred = Date.now();
};
this.i = function wrapIterator(iterator) {
return function () {
counter++;
if (counter % 100 === 0) {
console.log(counter / 100 + ' hundred ' + moment.duration(Date.now() - hundred).asSeconds());
console.log('items total: ' + counter);
hundred = Date.now();
}
iterator.apply(this, arguments);
};
};
this.c = function wrapCallback(cb, argsLength) {
return function (err, res) {
if (argsLength === 1) {
res = err; err = null;
}
var end = Date.now();
console.log('items total: ' + counter);
console.log('all: ' + moment.duration(end - start).asSeconds());
self.reset();
if (res) {
console.log('Result ' + (res.length ? 'length: ' + res.length : res));
}
cb(err, res);
};
};
}
var l = new Logger();
function each(apps, cb) {
l.reset();
apps.each(
l.i(function iterator(app, cb) {
cb();
}),
l.c(function onAllItems() {
cb();
}));
}
function map(apps, cb) {
l.reset();
function iterator(app, cb) {
cb(null, app.name);
}
async.series([
function (cb) {
apps.map(l.i(iterator), l.c(cb));
},
function (cb) {
apps.mapSeries(l.i(iterator), l.c(cb));
},
function (cb) {
apps.mapLimit(2, l.i(iterator), l.c(cb));
}],
cb
);
}
function filter(apps, cb) {
l.reset();
var i = 0;
function iterator(app, cb) {
cb(i++ % 2 === 0);
}
async.series([
function (cb) {
apps.filter(l.i(iterator), l.c(cb, 1));
},
function (cb) {
apps.filterSeries(l.i(iterator), l.c(cb, 1));
}
], cb);
}
function reject(apps, cb) {
l.reset();
var i = 0;
function iterator(app, cb) {
cb(i++ % 2 === 0);
}
async.series([
function (cb) {
apps.reject(l.i(iterator), l.c(cb, 1));
},
function (cb) {
apps.rejectSeries(l.i(iterator), l.c(cb, 1));
}
], cb);
}
function reduce(apps, cb) {
l.reset();
var i = 0;
function iterator(counter, app, cb) {
cb(null, i++);
}
async.series([
function (cb) {
apps.reduce(0, l.i(iterator), l.c(cb));
},
function (cb) {
apps.reduceRight(0, l.i(iterator), l.c(cb));
}
], cb);
}
function detect(apps, cb) {
l.reset();
var i = 0;
function iterator(app, cb) {
cb(0 === i++ % 2);
}
async.series([
function (cb) {
apps.detect(l.i(iterator), l.c(cb, 1));
},
function (cb) {
apps.detectSeries(l.i(iterator), l.c(cb, 1));
}
], cb);
}
function some(apps, cb) {
l.reset();
var i = 0;
function iterator(app, cb) {
cb(0 === i++ % 2);
}
apps.some(l.i(iterator), l.c(cb, 1));
}
function every(apps, cb) {
l.reset();
var i = 0;
function iterator(app, cb) {
cb(0 === i++ % 2);
}
apps.every(l.i(iterator), l.c(cb, 1));
}
function sortBy(apps, cb) {
function iterator(app, cb) {
cb(null, app.name);
}
apps.sortBy(l.i(iterator), l.c(cb));
}
function concat(apps, cb) {
function iterator(app, cb) {
cb(null, {app: app.name});
}
async.series([
function (cb) {
apps.concat(l.i(iterator), l.c(cb));
},
function (cb) {
apps.concatSeries(l.i(iterator), l.c(cb));
}
], cb);
}