Skip to content

Commit f29be01

Browse files
committed
update benchmarks, remove attach/detach in v-repeat as its actually expensive in common use cases
1 parent dc17a4e commit f29be01

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

examples/todomvc/js/benchmark.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,65 @@ if (navigator.userAgent.indexOf('PhantomJS') === -1) {
1010

1111
function runBenchmark () {
1212

13-
var now = window.performance && window.performance.now
13+
var itemsToAdd = 200,
14+
now = window.performance && window.performance.now
1415
? function () { return window.performance.now(); }
1516
: Date.now,
16-
beforeRender = now(),
17+
beforeBoot = now(),
1718
render,
18-
sync,
19-
async
19+
bench,
20+
addTime,
21+
toggleTime,
22+
removeTime
2023

2124
setTimeout(function () {
2225

23-
render = now() - beforeRender
26+
boot = now() - beforeBoot
2427

2528
var start = now(),
26-
newTodo = '12345'
29+
last
2730

28-
for (var i = 0; i < 100; i++) {
29-
app.newTodo = newTodo
30-
app.addTodo()
31+
add()
32+
33+
function add() {
34+
last = now()
35+
var newTodo = '12345'
36+
for (var i = 0; i < itemsToAdd; i++) {
37+
app.newTodo = newTodo
38+
app.addTodo()
39+
}
40+
setTimeout(toggle, 0)
3141
}
32-
setTimeout(toggle, 0)
3342

3443
function toggle () {
44+
addTime = now() - last
3545
var checkboxes = document.querySelectorAll('.toggle')
3646
for (var i = 0; i < checkboxes.length; i++) {
3747
checkboxes[i].click()
3848
}
39-
setTimeout(del, 0)
49+
last = now()
50+
setTimeout(remove, 0)
4051
}
4152

42-
function del () {
53+
function remove () {
54+
toggleTime = now() - last
4355
var deleteButtons = document.querySelectorAll('.destroy');
4456
for (var i = 0; i < deleteButtons.length; i++) {
4557
deleteButtons[i].click()
4658
}
47-
report()
59+
last = now()
60+
setTimeout(report, 0)
4861
}
4962

5063
function report () {
51-
sync = now() - start
52-
setTimeout(function () {
53-
async = now() - start
54-
console.log('render: ' + render.toFixed(2) + 'ms')
55-
console.log('sync: ' + sync.toFixed(2) + 'ms')
56-
console.log('async: ' + async.toFixed(2) + 'ms')
57-
}, 0)
64+
bench = now() - start
65+
removeTime = now() - last
66+
console.log('Benchmark x ' + itemsToAdd)
67+
console.log('boot : ' + boot.toFixed(2) + 'ms')
68+
console.log('add : ' + addTime.toFixed(2) + 'ms')
69+
console.log('toggle : ' + toggleTime.toFixed(2) + 'ms')
70+
console.log('remove : ' + removeTime.toFixed(2) + 'ms')
71+
console.log('total : ' + bench.toFixed(2) + 'ms')
5872
}
5973
}, 0)
6074

src/directives/repeat.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ module.exports = {
107107
self.collection = null
108108
self.vms = null
109109
self.mutationListener = function (path, arr, mutation) {
110-
self.detach()
111110
var method = mutation.method
112111
mutationHandlers[method].call(self, mutation)
113112
if (method !== 'push' && method !== 'pop') {
114113
self.updateIndexes()
115114
}
116-
self.retach()
117115
}
118116

119117
},
@@ -140,11 +138,9 @@ module.exports = {
140138

141139
// create child-vms and append to DOM
142140
if (collection.length) {
143-
this.detach()
144141
for (var i = 0, l = collection.length; i < l; i++) {
145142
this.buildItem(collection[i], i)
146143
}
147-
this.retach()
148144
}
149145
},
150146

@@ -209,31 +205,6 @@ module.exports = {
209205
}
210206
},
211207

212-
/**
213-
* Detach/retach the container from the DOM before mutation
214-
* so that batch DOM updates are done in-memory and faster
215-
*/
216-
detach: function () {
217-
if (this.hasTrans) return
218-
var c = this.container,
219-
p = this.parent = c.parentNode
220-
this.next = c.nextSibling
221-
if (p) p.removeChild(c)
222-
},
223-
224-
retach: function () {
225-
if (this.hasTrans) return
226-
var n = this.next,
227-
p = this.parent,
228-
c = this.container
229-
if (!p) return
230-
if (n) {
231-
p.insertBefore(c, n)
232-
} else {
233-
p.appendChild(c)
234-
}
235-
},
236-
237208
unbind: function () {
238209
if (this.collection) {
239210
this.collection.__observer__.off('mutate', this.mutationListener)

0 commit comments

Comments
 (0)