2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-05-17 04:29:37 +03:00

feat: add ssr benchmark for v3

This commit is contained in:
pimlie
2019-06-12 13:42:50 +02:00
parent dc165068c6
commit decc007c06
3 changed files with 137 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import v2 from './v2'
import { v3, v3cached, v3binding } from './v3'
const metaInfo = {
title: 'the title',
meta: [
{ charset: 'utf-8' },
{ name: 'description', content: 'the description' },
{ name: 'og:description', content: 'the description' },
{ name: 'twitter:description', content: 'the description' },
],
script: [
{ src: '/script.hs' }
],
noscript: [
{ innerHTML: 'no script' }
]
}
const count = 10000
const suites = {
v2,
v3,
v3cached,
v3binding
}
async function run() {
for (const suite of Object.keys(suites)) {
const data = []
for (let i = 0; i < count; i++) {
data.push(JSON.parse(JSON.stringify({
...metaInfo,
title: metaInfo.title + i
})))
}
let s = new Date().getTime()
for (let i = 0; i < count; i++) {
const html = await suites[suite](data[i])
// console.log(html)
}
const t = new Date().getTime() - s
console.log(`${suite}:`, t, 'ms')
}
}
run()
+19
View File
@@ -0,0 +1,19 @@
import { defaultOptions as options } from '../src/shared/constants'
import generateServerInjector from '../src/server/generateServerInjector'
export default function v2(metaInfo) {
// generate server injectors
for (const key in metaInfo) {
if (metaInfo.hasOwnProperty(key)) {
metaInfo[key] = generateServerInjector('ssr', options, key, metaInfo[key])
}
}
let text = ''
for (const key in metaInfo) {
text += metaInfo[key].text()
}
return `<head>${text}</head>`
}
+66
View File
@@ -0,0 +1,66 @@
import Vue from 'vue'
import { promisify } from 'util'
import LRU from 'lru-cache'
import { createRenderer, createBundleRenderer } from 'vue-server-renderer'
const renderToString = promisify(createRenderer().renderToString)
const cachedToString = promisify(createRenderer({
cache: new LRU()
}).renderToString)
function renderChilds(h, metaInfo) {
const children = []
Object.keys(metaInfo).forEach(k => {
if (Array.isArray(metaInfo[k])) {
metaInfo[k].forEach(attrs => {
const html = attrs.innerHTML
delete attrs.innerHTML
children.push(h(k, { attrs }, html))
})
return
}
if (k === 'title') {
children.push(h('title', null, metaInfo[k]))
}
})
return children
}
export async function v3(metaInfo) {
const head = new Vue({
render(h) {
return h('head', null, renderChilds(h, metaInfo))
}
})
return await renderToString(head)
}
export async function v3cached(metaInfo) {
const head = new Vue({
render(h) {
return h('head', null, renderChilds(h, metaInfo))
}
})
return await cachedToString(head)
}
const head = new Vue({
data: {
metaInfo: {}
},
render(h) {
const metaInfo = this.metaInfo
return h('head', null, renderChilds(h, metaInfo))
}
})
export async function v3binding(metaInfo) {
head.metaInfo = metaInfo
return await cachedToString(head)
}