-
Notifications
You must be signed in to change notification settings - Fork 2
/
perf.js
207 lines (175 loc) · 5.28 KB
/
perf.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
var LinkedList = require('./');
var randomString = require("random-string");
var chalk = require("chalk");
var InputValues = [];
var DeleteValues = [];
var count = 100000;
var chalkTitle = chalk.blue.bgWhite;
var chalkTime = (ms) => {
var str = ms + "ms";
if(ms <= 1000) {
return chalk.green(str);
} else {
return chalk.red(str);
}
}
var Timer = () => {
return new Date().getTime();
};
var MeasureSLL = () => {
var currString;
for(x=0; x<count; x++) {
currString = randomString();
InputValues.push(currString);
if(x % 10 === 0) {
DeleteValues.push(currString);
}
}
var start, end;
start = Timer();
var myList = new LinkedList();
for(x=0; x<count; x++) {
myList.insert(InputValues[x]);
}
end = Timer();
console.log("LinkedList insert " + count + " strings: " + chalkTime(end - start));
start = Timer();
for(x=0; x < DeleteValues.length; x++) {
myList.remove(DeleteValues[x]);
}
end = Timer();
console.log("LinkedList remove " + DeleteValues.length + " strings: " + chalkTime(end - start));
start=Timer();
var result = myList.toArray();
end=Timer();
console.log("Map sort time: " + chalkTime(end - start));
}
var MeasureBasicArray = () => {
var currString;
for(x=0; x<count; x++) {
currString = randomString();
InputValues.push(currString);
if(x % 10 === 0) {
DeleteValues.push(currString);
}
}
var start, end;
start = Timer();
var myArray = new Array();
for(x=0; x<count; x++) {
var index = 0;
while(!!myArray[index] && InputValues[x]>myArray[index]) index++;
myArray.splice(index, 0, InputValues[x]);
}
end = Timer();
console.log("Array insert " + count + " strings: " + chalkTime(end - start));
start = Timer();
for(x=0; x<DeleteValues.length; x++) {
var index = 0;
while(!!myArray[index] && DeleteValues[x]>myArray[index]) index++;
if(!!myArray[index] && myArray[index] === DeleteValues[x]) myArray.splice(index, 1);
}
end = Timer();
console.log("Array remove " + DeleteValues.length + " strings: " + chalkTime(end - start));
start=Timer();
var mapAsc = myArray;
end=Timer();
console.log("Array sort time: " + chalkTime(end - start));
}
var MeasureMap = () => {
var currString;
for(x=0; x<count; x++) {
currString = randomString();
InputValues.push(currString);
if(x % 10 === 0) {
DeleteValues.push(currString);
}
}
var start, end;
start = Timer();
var myMap = new Map();
for(x=0; x<count; x++) {
myMap.set(InputValues[x]);
}
end = Timer();
console.log("Map insert " + count + " strings: " + chalkTime(end - start));
start = Timer();
for(x=0; x<DeleteValues.length; x++) {
myMap.delete(DeleteValues[x]);
}
end = Timer();
console.log("Map remove " + DeleteValues.length + " strings: " + chalkTime(end - start));
start=Timer();
var mapAsc = [...myMap.entries()].sort();
end=Timer();
console.log("Map sort time: " + chalkTime(end - start));
}
var MeasureSortedMap = () => {
var SortedMap = require("collections/sorted-map");
var currString;
for(x=0; x<count; x++) {
currString = randomString();
InputValues.push(currString);
if(x % 10 === 0) {
DeleteValues.push(currString);
}
}
var start, end;
start = Timer();
var myMap = new SortedMap();
for(x=0; x<count; x++) {
myMap.set(InputValues[x]);
}
end = Timer();
console.log("SortedMap insert " + count + " strings: " + chalkTime(end - start));
start = Timer();
for(x=0; x<DeleteValues.length; x++) {
myMap.delete(DeleteValues[x]);
}
end = Timer();
console.log("SortedMap remove " + DeleteValues.length + " strings: " + chalkTime(end - start));
start=Timer();
var mapAsc = myMap.toJSON();
end=Timer();
console.log("SoredMap sort time: " + chalkTime(end - start));
}
MeasureSortedArray = () => {
var SortedArray = require("collections/sorted-array");
var currString;
for(x=0; x<count; x++) {
currString = randomString();
InputValues.push(currString);
if(x % 10 === 0) {
DeleteValues.push(currString);
}
}
var start, end;
start = Timer();
var myArray = new SortedArray();
for(x=0; x<count; x++) {
myArray.add(InputValues[x]);
}
end = Timer();
console.log("SortedArray insert " + count + " strings: " + chalkTime(end - start));
start = Timer();
for(x=0; x<DeleteValues.length; x++) {
myArray.delete(DeleteValues[x]);
}
end = Timer();
console.log("SortedArray remove " + DeleteValues.length + " strings: " + chalkTime(end - start));
start=Timer();
var mapAsc = myArray.toArray();
end=Timer();
console.log("SortedArray sort time: " + chalkTime(end - start));
}
//--------------------------
console.log(chalkTitle("Sorted Linked List"));
MeasureSLL();
console.log(chalkTitle("Maps"));
MeasureMap();
console.log(chalkTitle("SortedMaps"));
MeasureSortedMap();
console.log(chalkTitle("SortedArray"));
MeasureSortedArray();
console.log(chalkTitle("BasicArray"));
MeasureBasicArray();