vue2/benchmarks/reorder-list/index.html

103 lines
2.2 KiB
HTML
Raw Normal View History

2016-04-16 09:39:07 +08:00
<script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.min.js"></script>
<script src="../../dist/vue.min.js"></script>
2016-04-16 09:39:07 +08:00
<style>
.danger {
background-color: red;
}
</style>
2016-04-17 02:38:11 +08:00
<script type="text/x-template" id="t">
<div>
2016-04-17 04:14:45 +08:00
<h1>{{ total }} Components</h1>
2016-04-17 02:38:11 +08:00
<p>{{ action }} took {{time}}ms.</p>
<button @click="shuffle">shuffle</button>
<button @click="add">add</button>
<table class="table table-hover table-striped test-data">
<row v-for="item in items" track-by="id"
:class="{ danger: item.id === selected }"
:item="item"
@select="select(item)"
@remove="remove(item)">
</row>
</table>
</div>
2016-04-16 09:39:07 +08:00
</script>
2016-04-17 02:38:11 +08:00
<script type="text/x-template" id="row">
<tr>
<td class="col-md-1">{{item.id}}</td>
<td class="col-md-4">
<a @click="$emit('select')">{{item.label}}</a>
</td>
<td class="col-md-1">
<button @click="$emit('remove')">remove</button>
</td>
</tr>
2016-04-16 09:39:07 +08:00
</script>
<div id="el">
</div>
<script>
2016-04-17 04:14:45 +08:00
var total = 1000
2016-04-16 09:39:07 +08:00
var items = []
2016-04-17 04:14:45 +08:00
for (var i = 0; i < total; i++) {
2016-04-16 09:39:07 +08:00
items.push({
id: i,
label: String(Math.random()).slice(0, 5)
})
}
var s = window.performance.now()
2016-04-17 06:31:08 +08:00
console.profile('render')
2016-04-16 09:39:07 +08:00
var vm = new Vue({
el: '#el',
template: '#t',
data: {
2016-04-17 04:14:45 +08:00
total: total,
2016-04-16 09:39:07 +08:00
time: 0,
2016-04-17 02:38:11 +08:00
action: 'Render',
2016-04-16 09:39:07 +08:00
items: items,
selected: null
},
methods: {
2016-04-17 02:38:11 +08:00
shuffle: monitor('shuffle', function () {
2016-04-16 09:39:07 +08:00
this.items = _.shuffle(this.items)
}),
2016-04-17 02:38:11 +08:00
add: monitor('add', function () {
2016-04-16 09:39:07 +08:00
this.items.push({
2016-04-17 04:14:45 +08:00
id: total++,
2016-04-16 09:39:07 +08:00
label: String(Math.random()).slice(0, 5)
})
}),
2016-04-17 02:38:11 +08:00
select: monitor('select', function (item) {
2016-04-16 09:39:07 +08:00
this.selected = item.id
}),
2016-04-17 02:38:11 +08:00
remove: monitor('remove', function (item) {
2016-04-22 00:40:42 +08:00
this.items.splice(this.items.indexOf(item), 1)
2016-04-16 09:39:07 +08:00
})
},
components: {
row: {
props: ['item'],
template: '#row'
}
}
})
setTimeout(function () {
vm.time = window.performance.now() - s
2016-04-17 06:31:08 +08:00
console.profileEnd('render')
2016-04-16 09:39:07 +08:00
}, 0)
2016-04-17 02:38:11 +08:00
function monitor (action, fn) {
2016-04-16 09:39:07 +08:00
return function () {
var s = window.performance.now()
fn.apply(this, arguments)
Vue.nextTick(function () {
2016-04-17 02:38:11 +08:00
vm.action = action
2016-04-16 09:39:07 +08:00
vm.time = window.performance.now() - s
})
}
}
</script>