admin管理员组

文章数量:1403352

Let's say I have the following TypeScript code (represented as a string):

function greet(name: string): void {
  console.log(`Hello ${name}!`);
}

How would I programmatically determine how many kilobytes there are in this string?

I'm currently using the following equation:

// NOTE: "string.length" represents the number of bytes in the string
const KB: number = (string.length / 1024).toFixed(2);

The problem is that the number often appears to be far too big or far too small to be correct.

When I put the string in an empty file and save it, my file manager's properties output a pletely different size, sometimes it's off by 2-20 KB.

What am I doing wrong, should I be using 1000 bytes to represent a kilobyte instead of 1024?

Let's say I have the following TypeScript code (represented as a string):

function greet(name: string): void {
  console.log(`Hello ${name}!`);
}

How would I programmatically determine how many kilobytes there are in this string?

I'm currently using the following equation:

// NOTE: "string.length" represents the number of bytes in the string
const KB: number = (string.length / 1024).toFixed(2);

The problem is that the number often appears to be far too big or far too small to be correct.

When I put the string in an empty file and save it, my file manager's properties output a pletely different size, sometimes it's off by 2-20 KB.

What am I doing wrong, should I be using 1000 bytes to represent a kilobyte instead of 1024?

Share Improve this question edited Jul 2, 2019 at 17:36 Malekai asked May 2, 2019 at 17:51 MalekaiMalekai 5,0516 gold badges31 silver badges64 bronze badges 6
  • string.length is half the number of bytes in the string. Each character is two bytes. – Pointy Commented May 2, 2019 at 17:56
  • And you'll have to post more code and an example of how it's not working. There's nothing mysterious about getting the length of a string or dividing a number by 1024. – Pointy Commented May 2, 2019 at 17:58
  • This probably has little to do with TypeScript... seems like a runtime-only question. In what encoding are you saving the string? The length of a string in JS represents how many UTF-16 code units the string takes up. If you are saving the string in UTF-16, then the length is probably close to half the number of bytes of the file. But you're more likely to be using UTF-8, which can be quite different. – jcalz Commented May 2, 2019 at 17:58
  • @Pointy would it still be half the number of bytes if it was read from a file with a different encoding? – Malekai Commented May 2, 2019 at 18:03
  • @jcalz I'm saving the string in a file with a UTF-8 encoding. – Malekai Commented May 2, 2019 at 18:03
 |  Show 1 more ment

1 Answer 1

Reset to default 5

A character in JavaScript string is encoded using Unicode, every engine has their own character set, the most popular one being UTF-16. Therefore, each character holds 2 bytes of data. To find the total kilobytes being used by a string, find the number of bytes being used and divide it by 1024

const string = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc";

const b = string.length * 2;
const kb = (b / 1024).toFixed(2);

console.log(`${kb}KB`);

本文标签: