Skip to content

Commit 4c616f7

Browse files
committed
updated tests
1 parent 593e123 commit 4c616f7

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

test/js-diff-benchmark.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ libs.forEach((lib, i) => {
5959
var code = fs.readFileSync(require.resolve(file), 'utf8');
6060
var gzip = gzipSize.sync(Terser.minify(code).code);
6161

62+
// clean up the parent
6263
// clean up the parent
6364
parent.textContent = '';
6465
if (before)
@@ -171,7 +172,7 @@ libs.forEach((lib, i) => {
171172

172173
start();
173174
childNodes = create1000(parent, diff, childNodes);
174-
stop(parent.operations.length, 1000);
175+
stop(parent.mutations.length, 1000);
175176
console.assert(
176177
verifyNodes(parent, childNodes, 1000),
177178
'%s 1k',
@@ -180,7 +181,7 @@ libs.forEach((lib, i) => {
180181

181182
start();
182183
childNodes = create1000(parent, diff, childNodes);
183-
stop(parent.operations.length, 2000);
184+
stop(parent.mutations.length, 2000);
184185
console.assert(
185186
verifyNodes(parent, childNodes, 1000),
186187
'%s replace',
@@ -189,7 +190,7 @@ libs.forEach((lib, i) => {
189190

190191
start();
191192
childNodes = random(parent, diff, childNodes);
192-
stop(parent.operations.length, 1000);
193+
stop(parent.mutations.length, 2000);
193194
console.assert(
194195
verifyNodes(parent, childNodes, 1000),
195196
'%s random',
@@ -198,7 +199,7 @@ libs.forEach((lib, i) => {
198199

199200
start();
200201
childNodes = reverse(parent, diff, childNodes);
201-
stop(parent.operations.length, 1000);
202+
stop(parent.mutations.length, 2000);
202203
console.assert(
203204
verifyNodes(parent, childNodes, 1000),
204205
'%s reverse',
@@ -207,7 +208,7 @@ libs.forEach((lib, i) => {
207208

208209
start();
209210
childNodes = clear(parent, diff, childNodes);
210-
stop(parent.operations.length, 1000);
211+
stop(parent.mutations.length, 1000);
211212
console.assert(
212213
verifyNodes(parent, childNodes, 0),
213214
'%s clear',
@@ -218,7 +219,7 @@ libs.forEach((lib, i) => {
218219

219220
start();
220221
childNodes = append1000(parent, diff, childNodes);
221-
stop(parent.operations.length, 2000);
222+
stop(parent.mutations.length, 2000);
222223
console.assert(
223224
verifyNodes(parent, childNodes, 2000),
224225
'%s append 1k',
@@ -227,7 +228,7 @@ libs.forEach((lib, i) => {
227228

228229
start();
229230
childNodes = prepend1000(parent, diff, childNodes);
230-
stop(parent.operations.length, 1000);
231+
stop(parent.mutations.length, 1000);
231232
console.assert(
232233
verifyNodes(parent, childNodes, 3000),
233234
'%s prepend 1k',
@@ -239,7 +240,7 @@ libs.forEach((lib, i) => {
239240

240241
start();
241242
childNodes = swapRows(parent, diff, childNodes);
242-
stop(parent.operations.length, 2);
243+
stop(parent.mutations.length, 4);
243244
console.assert(
244245
parent.childNodes[1].textContent == 998 &&
245246
parent.childNodes[998].textContent == 1 &&
@@ -250,7 +251,7 @@ libs.forEach((lib, i) => {
250251

251252
start();
252253
childNodes = updateEach10thRow(parent, diff, childNodes);
253-
stop(parent.operations.length, 200);
254+
stop(parent.mutations.length, 200);
254255
console.assert(
255256
verifyNodes(parent, childNodes, 1000),
256257
'%s update 10th',
@@ -261,7 +262,7 @@ libs.forEach((lib, i) => {
261262

262263
start();
263264
childNodes = create10000(parent, diff, childNodes);
264-
stop(parent.operations.length, 10000);
265+
stop(parent.mutations.length, 10000);
265266
console.assert(
266267
verifyNodes(parent, childNodes, 10000),
267268
'%s 10k',
@@ -270,7 +271,7 @@ libs.forEach((lib, i) => {
270271

271272
start();
272273
childNodes = swapRows(parent, diff, childNodes);
273-
stop(parent.operations.length, 2);
274+
stop(parent.mutations.length, 4);
274275
console.assert(
275276
parent.childNodes[1].textContent == 9998 &&
276277
parent.childNodes[9998].textContent == 1 &&
@@ -317,36 +318,41 @@ function instrument(parent) {
317318
removeChild,
318319
replaceChild
319320
} = parent;
320-
parent.operations = [];
321+
parent.mutations = [];
321322
parent.appendChild = function (newNode) {
322-
this.operations.push(`appendChild(${newNode.textContent})`);
323+
const {textContent} = newNode;
324+
if (newNode.parentNode)
325+
this.mutations.push(`append: drop(${textContent})`);
326+
this.mutations.push(`append: add(${textContent})`);
323327
return appendChild.call(this, newNode);
324328
};
325329
parent.insertBefore = function (newNode, oldNode) {
326-
this.operations.push(
330+
const {textContent} = newNode;
331+
if (newNode.parentNode)
332+
this.mutations.push(`insert: drop(${textContent})`);
333+
this.mutations.push(
327334
oldNode ?
328-
`insertBefore(${newNode.textContent}, ${oldNode.textContent})` :
329-
`insertBefore(${newNode.textContent})`
335+
`insert: put(${textContent}) before (${oldNode.textContent})` :
336+
`insert: add(${textContent})`
330337
);
331338
return insertBefore.call(this, newNode, oldNode);
332339
};
333340
parent.removeChild = function (oldNode) {
334-
this.operations.push(`removeChild(${oldNode.textContent})`);
341+
this.mutations.push(`remove: drop(${oldNode.textContent})`);
335342
return removeChild.call(this, oldNode);
336343
};
337344
parent.replaceChild = function (newNode, oldNode) {
338-
this.operations.push(
339-
`delete#replaceChild(${newNode.textContent}, ${oldNode.textContent})`
340-
);
341-
this.operations.push(
342-
`insert#replaceChild(${newNode.textContent}, ${oldNode.textContent})`
343-
);
345+
const {textContent} = newNode;
346+
this.mutations.push(`replace: drop(${oldNode.textContent})`);
347+
if (newNode.parentNode)
348+
this.mutations.push(`replace: drop(${textContent})`);
349+
this.mutations.push(`replace: put(${textContent})`);
344350
return replaceChild.call(this, newNode, oldNode);
345351
};
346352
}
347353

348354
function reset(parent) {
349-
parent.operations.splice(0);
355+
parent.mutations.splice(0);
350356
}
351357

352358
function round(num) {

0 commit comments

Comments
 (0)