import { buildData, startMeasure, stopMeasure, formatNumber } from "../shared/utils.js"; //------------------------------------------------------------------------------ // Counter Widget //------------------------------------------------------------------------------ Vue.component("likes-counter", { data: function() { return { counter: 0 }; }, methods: { increment() { this.counter++; } }, template: `
` }); //------------------------------------------------------------------------------ // Message Widget //------------------------------------------------------------------------------ Vue.component("my-message", { props: ["msg"], methods: { removeMessage() { this.$emit("removeMessage", this.msg.id); } }, template: `
{{msg.author}} {{msg.msg}}
` }); //------------------------------------------------------------------------------ // Root Widget //------------------------------------------------------------------------------ const App = { name: "App", data() { return { messages: [], multipleFlag: false, clearAfterFlag: false }; }, mounted() { this.log(`Benchmarking Vue v${Vue.version}`); }, methods: { addMessages(n) { this.benchmark("add " + n, () => { const newMessages = buildData(n); this.messages.push.apply(this.messages, newMessages); }); }, clear() { this._benchmark("clear", () => { this.messages = []; }); }, clearLog() { this.$refs.log.innerHTML = ""; }, updateSomeMessages() { this.benchmark("update every 10th", () => { const messages = this.messages; for (let i = 0; i < this.messages.length; i += 10) { messages[i].author += "!!!"; } }); }, removeMessage(id) { startMeasure("remove message"); const index = this.messages.findIndex(m => m.id === id); this.messages.splice(index, 1); stopMeasure(); }, log(str, isBold) { const div = document.createElement("div"); if (isBold) { div.classList.add("bold"); } div.textContent = `> ${str}`; this.$refs.log.appendChild(div); this.$refs.log.scrollTop = this.$refs.log.scrollHeight; }, benchmark(message, fn, callback) { if (this.multipleFlag) { const N = 20; let n = N; let total = 0; let cb = info => { let finalize = () => { n--; total += info.delta; if (n === 0) { const avg = total / N; this.log(`Average: ${formatNumber(avg)}ms`, true); if (callback) { callback(); } } else { this._benchmark(message, fn, cb); } }; if (this.clearAfterFlag) { this._benchmark( "clear", () => { this.messages = []; }, finalize, false ); } else { finalize(); } }; this._benchmark(message, fn, cb); } else { this._benchmark(message, fn, callback); } }, _benchmark(message, fn, cb, log = true) { setTimeout(() => { startMeasure(message); fn(); stopMeasure(info => { if (log) { this.log(info.msg); } if (cb) { cb(info); } }); }, 10); } }, template: `
Actions
Number of messages: {{messages.length}}

Log (clear)
` }; //------------------------------------------------------------------------------ // Application initialization //------------------------------------------------------------------------------ new Vue({ render: h => h(App) }).$mount(`#main`);