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
?
-
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
1 Answer
Reset to default 5A 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`);
本文标签:
版权声明:本文标题:javascript - How do I determine how many kilobytes there are in a string of code using TypeScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744373042a2603123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论