diff --git a/src/dom/Danger.js b/src/dom/Danger.js index d3d5ab3ab6..68f365c624 100644 --- a/src/dom/Danger.js +++ b/src/dom/Danger.js @@ -31,6 +31,9 @@ var mutateHTMLNodeWithMarkup = require('mutateHTMLNodeWithMarkup'); var COMMENT_NODE_TYPE = 8; +// This buffer will be reused in dangerouslyRenderMarkup to avoid unnecessary +// array allocations. +var reusableBuffer = []; /** * Extracts the `nodeName` from a string of markup. @@ -50,9 +53,9 @@ function getNodeName(markup) { var Danger = { /** - * Renders markup into an array of nodes. The markup is expected to - * render into a list of root nodes. Also, the length of `resultList` - * and `markupList` should be the same. + * Renders markup into an array of nodes. The markup is expected to render + * into a list of root nodes. Also, the length of `resultList` and + * `markupList` should be the same. * * @param {array} markupList List of markup strings to render. * @return {array} List of rendered nodes. @@ -86,15 +89,17 @@ var Danger = { } var markupListByNodeName = markupByNodeName[nodeName]; - var commentedMarkupList = []; - // This for-in loop skips the holes of the sparse array. The order - // of iteration should follow the order of assignment, which happens - // to match numerical index order, but we don't rely on that. + var commentedMarkupList = reusableBuffer; + commentedMarkupList.length = 0; + + // This for-in loop skips the holes of the sparse array. The order of + // iteration should follow the order of assignment, which happens to match + // numerical index order, but we don't rely on that. for (var resultIndex in markupListByNodeName) { if (markupListByNodeName.hasOwnProperty(resultIndex)) { // Push the requested markup followed by a sentinel HTML - // comment. Comments are a good choice for sentinels because - // they can appear as children of any HTML node. + // comment. Comments are a good choice for sentinels because they can + // appear as children of any HTML node. commentedMarkupList.push( markupListByNodeName[resultIndex], // This resultIndex will be parsed back out below. @@ -103,14 +108,15 @@ var Danger = { } } - // Render each group of markup with similar wrapping `nodeName`, - // with sentinel comments interspersed. + // Render each group of markup with similar wrapping `nodeName`, with + // sentinel comments interspersed. var renderNodes = createNodesFromMarkup( commentedMarkupList.join(''), emptyFunction // Do nothing special with