-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Size difference between node and browser #82
Comments
OK, the difference is coming from here https://github.com/miktam/sizeof/blob/master/indexv2.js#L88 Node.js uses precise string calculation. Here is the PR #80 The browser uses quite a simplistic approach, assuming that every string char is 2 bytes. To be precise in the browser environment, let me check if there is a difference between different VM implementations. |
Hello, I've been doing some research and it seems that the best options are. For node: Buffer.byteLength(string); I think this library has a good approach: What I don't know is the compatibility that you give since that library is in node version >=14.18.0 Regarding TextEncoder it seems to have good compatibility: https://caniuse.com/?search=TextEncoder In the example that I have given, in both cases it gives a size of 171, which does not match what it gives now. With a complex emoji gives: 🏳️🌈 gives 14 I also don't know if it differs with Cyrillic, Arabic, Chinese characters, etc. Greetings |
@Marius-Romanus thank you for the investigation! Regarding node.js version, compatibility might be the issue, as you rightfully noted. |
Hello, Buffer.byteLength exists in Node since the first versions, but I think it has been modified many times and I don't know the expected result in each of them or possible errors: Although I imagine that you have already seen it but I leave you the documentation (you can pass the type of encoding): @ehmicky may have put the compatibility in for something else, or even for ECMAScript imports in Node. ;) Greetings |
Hi everyone, I am not completely sure I am answering your question correctly, but the reason this module does not support Node 12 is because Node 12 is not officially supported anymore. Also, please note Node 14 official support will be dropped in 2 months. The main advantage of using Also, I think you might want to distinguish UTF-8 and UTF-16 when discussing about sizes. A string only has a specific byte size for a given encoding. As pointed out in your README, the JavaScript specification considers strings to be conceptually "somewhat" UTF-16, i.e. each character is 2 bytes. I mentioned "somewhat" because surrogate characters (U+d800 to U+dfff) and astral characters (U+10000 and above) are handled a little differently, and it depends on the JavaScript method being used. However, in memory, over the network, or in a file, those strings are likely to be encoded in UTF-8, where each character can be 1, 2, 3 or 4 bytes long. If you're interested about this topic, I wrote the following article which details the differences. |
…string. Tested on node v12 - works. Does not work on node v10. Continuation of #82
ok, latest PR works in node v12, but does not work in v10. Let´s see if this is the best we can have. |
Hi, there is a size difference calculating the same string type between the browser and node.
I understand that being only a string and not having objects or anything weird, it should be the same size, right?
Greetings!.
console.log("node sizeof()", sizeof('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac vestibulum lacus, sit amet maximus libero. Aliquam erat volutpat. Quisque at orci tortor. Donec at mi nunc.'));
node sizeof() 184
console.log("browser sizeof()", sizeof('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac vestibulum lacus, sit amet maximus libero. Aliquam erat volutpat. Quisque at orci tortor. Donec at mi nunc.'));
browser sizeof() 342
The text was updated successfully, but these errors were encountered: