是否有一些技巧来编写dustjs模板以便更快地渲染?的node.js

时间:2016-03-05 08:13:57

标签: node.js render dust.js

我使用dustjs呈现我的邮件内容,我发现渲染更大的模板时速度很慢。在编写dustjs模板以提高渲染速度或一些好的做法时是否有任何提示?

细节: 我的模板由几个组件(超过5个)组成,每个组件如果与其他组件不同,则组件的一个示例如下:

<tr>
  <td align="center" valign="top" bgcolor="#FFFFFF">
    <table cellpadding="0" cellspacing="0" width="650">
      <tr>
        <td align="center">
          <table width="590" border="0" cellspacing="0" cellpadding="0">
            {@eq key=haveBanner value=1}
            <tr>
              <td height="105" align="center" valign="middle">
                <a href="{edm_monitor_server}/api/clickRecord?{.|sellerPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank"><img src="{bannerUrl|pictureUrl}" width="590" border="0"></a>
              </td>
            </tr>
            {:else}
            <tr>
              <td align="center">
                <table cellpadding="0" cellspacing="0" width="590" height="75" style="border-top:solid 3px #343434">
                  <tr>
                    <td width="130" align="left" valign="middle">
                      <table border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td align="left" height="55">
                            <a href="{edm_monitor_server}/api/clickRecord?{.|sellerPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank"><img src="{sellerLogo|pictureUrl}" width="110" height="55"></a>
                          </td>
                        </tr>
                      </table>
                    </td>
                    <td width="460" align="left">
                      <table border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td align="left"><a href="{edm_monitor_server}/api/clickRecord?{.|sellerPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank" style="text-decoration:none"><font style="font-family:arial;font-size:16px;color:#292929">{compName}</strong></a></td>
                        </tr>
                      </table>
                    </td>
                  </tr>

                </table>
              </td>
            </tr>
            {/eq}

            <tr>
              <td>
                <table border="0" cellpadding="0" cellspacing="0" style="border:solid 1px #dbdbdb" width="594">
                  <tr>
                    <td width="149" valign="top" style="border-right:solid 1px #dbdbdb">
                      <table width="146" cellspacing="0" cellpadding="0">
                        <tr>
                          {@eq key=selectedProductFlag value=1}
                          <td height="80" style="padding:20px 20px"><a href="{edm_monitor_server}/api/clickRecord?{.|sellerPromoteProdListPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank" style="text-decoration:none"><font style="font-family:arial;font-size:18px;color:#333333"><b>Promotion Products</b></font></a></td>
                          {:else}
                          <td height="80" style="padding:20px 20px"><a href="{edm_monitor_server}/api/clickRecord?{.|sellerProdListPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank" style="text-decoration:none"><font style="font-family:arial;font-size:18px;color:#333333"><b>Promotion Products</b></font></a></td>
                          {/eq}
                        </tr>

                        <tr>
                          <td align="center" height="26" valign="top">
                            <table width="119" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td style="border-top:solid 1px #e5e5e5"><img src="http://newimg.globalmarket.com/PA/edm/images/spacer.gif" height="1" width="1"></td>
                              </tr>
                            </table>
                          </td>
                        </tr>

                        {#finalProdGroupList}
                        <tr>
                          <td height="35" valign="top">
                            <table width="146" cellspacing="0" cellpadding="0">
                              <tr>
                                <td style="padding-left:25px"><a href="{edm_monitor_server}/api/clickRecord?{.|sellerGroupProdListPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank" style="text-decoration:none"><font style="font-family:arial;font-size:12px;color:#484848">{prodGroupName}</font></a></td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        {/finalProdGroupList}
                      </table>
                    </td>

                    <td width="443" valign="top">
                      <table width="442" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td>
                            <table width="442" border="0" cellspacing="0" cellpadding="0" height="255">
                              <tr>
                                {#secondaryProducts}
                                {#productList spIdx=$idx}
                                {@eq key=$idx value=2}
                                <td align="center" valign="top" width="147">
                                {:else}
                                <td align="center" valign="top" style="border-right:solid 1px #dbdbdb" width="147">
                                {/eq}
                                  <table border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td height="146" align="center">
                                        <a href="{edm_monitor_server}/api/clickRecord?{.|sellerProductPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank"><img src="{photoUrl|pictureUrl}" border="0" width="126" height="126" alt="{fullProdName}"></a>
                                      </td>
                                    </tr>
                                    <tr>
                                      <td align="center">
                                        <table width="126" border="0" cellspacing="0" cellpadding="0">
                                          <tr>
                                            <td align="center">
                                              <table cellpadding="0" cellspacing="0" width="126">
                                                <tr>
                                                  <td align="left" height="33" valign="top" style="line-height:15px">
                                                    <a href="{edm_monitor_server}/api/clickRecord?{.|sellerProductPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}" target="_blank" style="text-decoration:none"><font style="font-family:Arial;font-size:12px;color:#333333">{productName}</font></a>
                                                  </td>
                                                </tr>
                                                <tr>
                                                  <td align="left" height="20" valign="middle"><font style="font-family:Arial;font-size:12px;color:#999999">{certification}</font></td>
                                                </tr>
                                                <tr>
                                                  {@eq key=payForInqFlag value=1}
                                                  <td align="left" height="35" valign="middle"><a href="{edm_monitor_server}/api/clickRecord?{.|addInquiryPage}&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|taskId|edmpid|recId|userId|token|final}" title="Get Latest Price" style="color: #555555; text-decoration: none;" target="_blank"><img src="http://newimg.globalmarket.com/PA/edm/images/getprice2.png" width="128" height="25"></a></td>
                                                  {:else}
                                                  <td align="left" height="35" valign="middle"><a href="{edm_monitor_server}/api/clickRecord?{.|m2bProductPage}&type=getPrice&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|taskId|edmpid|recId|userId|token|final}" title="Get Latest Price" style="color: #555555; text-decoration: none;" target="_blank"><img src="http://newimg.globalmarket.com/PA/edm/images/getprice2.png" width="128" height="25"></a></td>
                                                  {/eq}
                                                </tr>

                                                <tr>
                                                  <td height="7"></td>
                                                </tr>
                                              </table>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                                {/productList}
                                {/secondaryProducts}
                              </tr>
                            </table>
                          </td>
                        </tr>
                      </table>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </td>
</tr>

1 个答案:

答案 0 :(得分:0)

您的问题很可能是您正在使用的大量灰尘过滤器。

示例:

&{trackCodeData|gaTrackCode|dbTrackCode|entityId|entityType|edmpid|recId|userId|token|final}

其中一些过滤器甚至在循环中运行。看起来每个渲染此模板的运行时间超过100个。过滤器是同步和连续执行的,因此在过滤完成之前,Dust无法继续渲染模板。

在不知道这些过滤器的作用的情况下,很难就过滤器本身提出具体建议。但是,您似乎多次使用相同的过滤器来输出相同的数据。如果可能,您应该预生成此数据并将其添加到上下文中,而不是动态生成它。

如果关闭空白压缩,你可以通过打开空格压缩来获得更多周期:

dust.config.whitespace = false;

但Dust本质上非常快。如果没有您的过滤器,您的模板将在3ms左右呈现给我。