title: How do I determine how many kilobytes there are in a string of code using TypeScript?
body: <p>Let's say I have the following TypeScript code (represented as a string):</p>
  
  <pre><code>function greet(name: string): void {
    console.log(`Hello ${name}!`);
  }
  </code></pre>
  
  <p>How would I programmatically determine how many kilobytes there are in this string?</p>
  
  <p>I'm currently using the following equation:</p>
  
  <pre><code>// NOTE: "string.length" represents the number of bytes in the string
  const KB: number = (string.length / 1024).toFixed(2);
  </code></pre>
  
  <p>The problem is that the number often appears to be far too big or far too small to be correct.</p>
  
  <p>When I put the string in an empty file and save it, my file manager's properties output a completely different size, sometimes it's off by 2-20 KB.</p>
  
  <p>What am I doing wrong, should I be using <code>1000</code> bytes to represent a kilobyte instead of <code>1024</code>?</p>