title: Most efficient way for rendering string templates using javascript
body: <p>I need to render a template string in my node.js application. I don't want to use Jade, Ejs, React or similar libraries. I want to do it using plain javascript for various reason. I wonder which is the most efficient way to do it, i have thought two ways. </p>
  
  <p>An example template string could be: </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  
   &lt;body&gt;
     &lt;div&gt;{{data1}}&lt;/div&gt;
     &lt;span&gt;{{data2}}&lt;/span&gt;
   &lt;/body&gt; 
  
  &lt;/html&gt;
  </code></pre>
  
  <p>I need to replace <code>{{data1}}</code> and <code>{{data2}}</code> with some data.
  I am wondering if it's a better solution in terms of performance using regexes and calling multiple times:</p>
  
  <p><code>templateString.replace(/{{data1}}/, data)</code> </p>
  
  <p>or cycling each character of the string and perform rendering inside a for loop in the following way, without using regexes:</p>
  
  <p><code>for(i = 0; i &lt; templateString.length; i++) {//binding logic}</code></p>
  
  <p>I don't know how <code>replace</code> method works at the lower level, so i wonder if performing it multiple time is worse in performance than cycling once all characters of my string.</p>
  
  <p>Thank you for any advice!</p>