From 93357b8f3478f1d0b2894c92160948b16b170890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Inez=20Korczyn=CC=81ski?= Date: Thu, 25 Apr 2013 14:04:51 -0700 Subject: [PATCH] Performance improvement for getRenderedContents Instead of calling $.append for every single char - buffer and call $.append only when really needed. Change-Id: I53acfa795ea5dc6a8ca39ce11017daa85c9151d2 --- modules/ve/ce/ve.ce.ContentBranchNode.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/modules/ve/ce/ve.ce.ContentBranchNode.js b/modules/ve/ce/ve.ce.ContentBranchNode.js index 80ba809c92..1b3666b317 100644 --- a/modules/ve/ce/ve.ce.ContentBranchNode.js +++ b/modules/ve/ce/ve.ce.ContentBranchNode.js @@ -78,9 +78,18 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { annotationStack = new ve.dm.AnnotationSet( store ), annotatedHtml = [], $wrapper = $( '
' ), - $current = $wrapper; + $current = $wrapper, + buffer = ''; + + function flushBuffer() { + if ( buffer !== '' ) { + $current.append( buffer ); + buffer = ''; + } + } function openAnnotation( annotation ) { + flushBuffer(); // Create a new DOM node and descend into it $ann = ve.ce.annotationFactory.create( annotation.getType(), annotation ).$; $current.append( $ann ); @@ -88,6 +97,7 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { } function closeAnnotation() { + flushBuffer(); // Traverse up $current = $current.parent(); } @@ -111,10 +121,15 @@ ve.ce.ContentBranchNode.prototype.getRenderedContents = function () { openAnnotation, closeAnnotation ); - // Output the actual HTML - $current.append( itemHtml ); + // Handle the actual HTML + if ( typeof itemHtml === 'string' ) { + buffer += itemHtml; + } else { + flushBuffer(); + $current.append( itemHtml ); + } } - + flushBuffer(); return $wrapper.contents(); };