-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (99 loc) · 2.68 KB
/
index.js
File metadata and controls
116 lines (99 loc) · 2.68 KB
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
const lodash = require('lodash');
const underscore = require('underscore');
const smallTestArray = [
"foo",
"bar",
1,
2,
3,
4,
5,
"foo",
5,
3,
2,
1,
"bar",
"baz",
"The quick brown fox",
543543,
5434,
432154,
3454,
2,
3,
{ a: 1},
[ 1, 2, 4 ],
9,
12,
12,
5,
3,
2,
1,
"bar",
"baz",
"The quick brown fox",
543543,
5434,
"bar",
1,
2,
3,
4,
5,
"foo",
"a",
"b",
"c",
{ a: 1},
[ 1, 2, 4 ],
"fun",
12
];
const mediumTestArray = Array.from({length: 100}, () => smallTestArray).reduce((a, c) => [...a, ...c], []);
const largeTestArray = Array.from({length: 1000}, () => smallTestArray).reduce((a, c) => [...a, ...c], []);
let target;
const testMe = (testFn, name, arr) => {
const limit = 10000;
const start = new Date();
for (let x = 0; x < limit; x++) {
target = testFn(arr);
}
const end = new Date();
console.log(` ${name}:`);
console.log(` Time elapsed: ${end - start}ms`);
console.log(` Items/ms: ${Math.round((limit * arr.length)/(end - start))}`);
console.log();
};
const lodashUniqTest = arr => lodash.uniq(arr);
const underscoreUniqTest = arr => underscore.uniq(arr);
const setTest = arr => Array.from(new Set(arr));
const objAssignTest = arr => Object.keys(arr.reduce((acc, curr) => Object.assign(acc, {[curr]: undefined}), {}));
const objAssignImmutableTest = arr => Object.keys(arr.reduce((acc, curr) => Object.assign({}, acc, {[curr]: undefined}), {}));
const objReduceTest = arr => Object.keys(arr.reduce((acc, curr) => {acc[curr] = undefined; return acc;}, {}));
const objForEachTest = arr => {
const obj = {};
arr.forEach(e => obj[e] = undefined);
return Object.keys(obj);
};
console.log("Small")
testMe(lodashUniqTest, "lodash.uniq", smallTestArray);
testMe(underscoreUniqTest, "underscore.uniq", smallTestArray);
testMe(setTest, "Set", smallTestArray);
testMe(objAssignTest, "Object assign", smallTestArray);
testMe(objAssignImmutableTest, "Object assign immutable", smallTestArray);
testMe(objReduceTest, "Object reduce", smallTestArray);
testMe(objForEachTest, "Object for each", smallTestArray);
console.log("Medium")
testMe(lodashUniqTest, "lodash.uniq", mediumTestArray);
testMe(underscoreUniqTest, "underscore.uniq", mediumTestArray);
testMe(setTest, "Set", mediumTestArray);
testMe(objAssignTest, "Object assign", mediumTestArray);
// testMe(objAssignImmutableTest, "Object assign immutable", mediumTestArray);
testMe(objReduceTest, "Object reduce", mediumTestArray);
testMe(objForEachTest, "Object for each", mediumTestArray);
console.log("Large")
// testMe(uniqTest, "Uniq", largeTestArray);
testMe(setTest, "Set", largeTestArray);
// // testMe(objTest, "Obj", largeTestArray);