From da1db7af31b397eab1608217d1f6dee26f753a59 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 14 May 2017 14:23:38 +0800 Subject: [PATCH] experiment --- benchmarks/ssr/common.js | 55 ++++++++++++++++++++++++++------ benchmarks/ssr/renderToString.js | 4 ++- src/server/render.js | 20 +++++++++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/benchmarks/ssr/common.js b/benchmarks/ssr/common.js index 4ce56fb0..9711a520 100644 --- a/benchmarks/ssr/common.js +++ b/benchmarks/ssr/common.js @@ -25,6 +25,8 @@ function generateGrid (rowCount, columnCount) { const gridData = generateGrid(1000, 10) +const five = [0, 1, 2, 3, 4] + module.exports = { template: '

{{ Math.random() }}

', components: { @@ -39,17 +41,34 @@ module.exports = { components: { row: { props: ['row'], - template: '{{ Math.random() }}', + // template: '{{ Math.random() }}', + render (h) { + return h('tr', [ + new TextNode(1, [ + new TextNode(2), + ...this.row.items.map(item => h('column')) + ]) + ]) + }, components: { column: { - template: '' + - // 25 plain elements for each cell - '' + - '' + render (h) { + return h('td', { class: 'item' }, [ + new TextNode(4, + five.map(() => new TextNode(5, + five.map(() => new TextNode(6, [new TextNode(7)]))) + ) + ) + ]) + } + // template: '' + + // // 25 plain elements for each cell + // '' + + // '' } } } @@ -57,3 +76,21 @@ module.exports = { } } } + +const renderFns = [ + { open: () => ``, close: `` }, + { open: () => ``, close: `` }, + { open: () => Math.random() }, + { open: () => ``, close: `` }, + { open: () => `` }, + { open: () => `
  • `, close: `
  • ` }, + { open: () => ``, close: `` }, + { open: () => `fsefs` } +] + +function TextNode (id, children) { + this.isTextNode = true + this.open = renderFns[id].open + this.close = renderFns[id].close + this.children = children +} diff --git a/benchmarks/ssr/renderToString.js b/benchmarks/ssr/renderToString.js index dbd63072..a96373e0 100644 --- a/benchmarks/ssr/renderToString.js +++ b/benchmarks/ssr/renderToString.js @@ -11,7 +11,9 @@ console.log('--- renderToString --- ') const self = (global || root) self.s = self.performance.now() -renderToString(new Vue(gridComponent), () => { +renderToString(new Vue(gridComponent), (err, res) => { + if (err) throw err + // console.log(res) console.log('Complete time: ' + (self.performance.now() - self.s).toFixed(2) + 'ms') console.log() }) diff --git a/src/server/render.js b/src/server/render.js index 3ebb3e67..76335a91 100644 --- a/src/server/render.js +++ b/src/server/render.js @@ -38,7 +38,9 @@ const normalizeRender = vm => { } function renderNode (node, isRoot, context) { - if (isDef(node.componentOptions)) { + if (node.isTextNode) { + renderTextNode(node, context) + } else if (isDef(node.componentOptions)) { renderComponent(node, isRoot, context) } else { if (isDef(node.tag)) { @@ -161,6 +163,22 @@ function renderComponentInner (node, isRoot, context) { renderNode(childNode, isRoot, context) } +function renderTextNode (el, context) { + const { write, next } = context + if (isUndef(el.children) || el.children.length === 0) { + write(el.open() + (el.close || ''), next) + } else { + const children: Array = el.children + context.renderStates.push({ + type: 'Element', + rendered: 0, + total: children.length, + endTag: el.close, children + }) + write(el.open(), next) + } +} + function renderElement (el, isRoot, context) { const { write, next } = context