This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
193 lines (164 loc) · 4.55 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
(function () {
function SimpleViewModel(data) {
this.name = ko.computed(data.name);
this.number = ko.computed(data.number);
this.array = ko.computed(data.array);
}
var state = {
models: ko.observableArray([]),
results: ko.observableArray()
};
function randInt(from, to) {
return Math.round(Math.random() * (to - from)) + from;
}
var NAMECHARS = "abcdefghijklmnopqrstuvxyz".split('');
function getChar() {
return NAMECHARS[randInt(1, NAMECHARS.length) - 1];
}
function makeName(length) {
var i, name = getChar().toUpperCase();
for (i = 0; i < length; i += 1) {
name += getChar();
}
return name;
}
function makeArray(length) {
var i, array = [];
for (i = 0; i < length; i += 1) {
array.push(randInt(0,9));
}
return array;
}
function makeModel() {
return {
name: ko.observable(makeName(randInt(3, 6))),
number: ko.observable(randInt(10, 99)),
array: ko.observableArray(makeArray(randInt(2, 5)))
};
}
function makeState(count) {
if (!count) count = 10;
var i, models = [];
for (i = 0; i < count; i += 1) {
models.push(makeModel());
}
state.models(models);
}
state.views = state.models.map(function (model) {
return new SimpleViewModel(model);
});
window.state = state;
document.addEventListener("DOMContentLoaded", function() {
ko.applyBindings(state);
});
function addResult(title, outcome) {
state.results.push({
title: title,
result: outcome + ''
});
}
function runTest(title, test) {
var startTime = new Date();
test();
var endTime = new Date();
addResult(title, endTime - startTime);
}
runTest("Simple viewmodel map, ten thousand items", function () {
makeState(10000);
});
runTest("Reverse", function() {
state.models.reverse();
});
var array = state.models();
var iterations = 5;
var startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 5 elements in batch.", function () {
state.models(array);
});
iterations = 50;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 50 elements in batch.", function () {
state.models(array);
});
iterations = 250;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 250 elements in batch.", function () {
state.models(array);
});
iterations = 500;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 500 elements in batch.", function () {
state.models(array);
});
iterations = 1000;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 1000 elements in batch.", function () {
state.models(array);
});
iterations = 3000;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
array[startPoint + iterations] = makeModel();
}
runTest("Replace 3000 elements in batch.", function () {
state.models(array);
});
runTest("Replace 5 elements individually.", function () {
var iterations = 5;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
runTest("Replace 50 elements individually.", function () {
var iterations = 50;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
runTest("Replace 250 elements individually.", function () {
var iterations = 250;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
runTest("Replace 500 elements individually.", function () {
var iterations = 500;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
runTest("Replace 1000 elements individually.", function () {
var iterations = 1000;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
runTest("Replace 3000 elements individually.", function () {
var iterations = 3000;
startPoint = randInt(0,9999 - iterations);
while (iterations--) {
state.models.replace(startPoint + iterations, makeModel());
}
});
makeState(10);
})();