admin管理员组文章数量:1287505
I need to try to estimate the DISK size of a text string (which could be raw text or a Base64 encoded string for an image/audio/etc) in JavaScript. I'm not sure how to estimate this. The only thing when Googling i can find is .length
so i thought maybe someone on StackOverflow might know...
The reason i need to know is i have a localStorage script that needs (or would love to have) the ability to check when a user is nearing his 5MB (or 10MB in IE) quota and prompt them to increase the max size for the domain. So, if a user hits, lets say, 4.5MBs of data it'd prompt with
You're nearing your browsers 5MB data cap. Please increase your max data by... [instructions on increasing it for the browser]
I need to try to estimate the DISK size of a text string (which could be raw text or a Base64 encoded string for an image/audio/etc) in JavaScript. I'm not sure how to estimate this. The only thing when Googling i can find is .length
so i thought maybe someone on StackOverflow might know...
The reason i need to know is i have a localStorage script that needs (or would love to have) the ability to check when a user is nearing his 5MB (or 10MB in IE) quota and prompt them to increase the max size for the domain. So, if a user hits, lets say, 4.5MBs of data it'd prompt with
Share Improve this question edited Nov 29, 2010 at 22:33 Oscar Godson asked Nov 29, 2010 at 22:20 Oscar GodsonOscar Godson 32.8k42 gold badges125 silver badges206 bronze badges 34You're nearing your browsers 5MB data cap. Please increase your max data by... [instructions on increasing it for the browser]
- 1 I am not sure what your question is. Can you rephrase? And: Why don't you try what you found? – jwueller Commented Nov 29, 2010 at 22:25
- 1 @SimpleCoder: Disc space? Are you talking about RAM? Then no, there is no way of determining that. At least not implementation-independent, since this is not part of the spec. – jwueller Commented Nov 29, 2010 at 22:28
- 1 @Darin Dimitrov; Don't patronize me. You are missing the point. The asker wants to estimate the disk space a string would occupy IF it was placed on a disk. – Chris Laplante Commented Nov 29, 2010 at 22:32
- 1 @SimpleCoder, OK, now that makes sense. The next question: what encoding? – Darin Dimitrov Commented Nov 29, 2010 at 22:36
- 2 @Darin Dimitrov: I would suggest you go read a book about puters sometime soon instead of stating that everybody around you is stupid. – GolezTrol Commented Nov 29, 2010 at 22:46
5 Answers
Reset to default 3It is going to depend on your character encoding. If you use ASCII encoding, it's going to be str.length bytes. If you use UTF-16, it's going to be (str.length * 2) bytes. If you use UTF-8, it is going to depend on the characters in the string. (Some characters will only take 1 byte, but others could take up to 4 bytes.) If you're dealing with Base64-encoded data, the characters are all within the ASCII range and therefore would occupy str.length bytes on disk. If you decode them first and save as binary, it would take (str.length * 3/4) bytes. (With Base64, 3 uncoded bytes bee 4 coded bytes.)
BTW - If you haven't read Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!), you should do so immediately.
http://www.joelonsoftware./articles/Unicode.html
UPDATE: If you're using localStorage, I assume that you're familiar with window.localStorage.length, though this only tells you how much has been used, not whether your new data will fit. I would also highly remend reading Dive into HTML5, especially the section on storage:
http://diveintohtml5.ep.io/storage.html
Unless something has changed since its writing, I'm not sure what you can do as localStorage limits you to 5MB per domain with no way for the user to increase it.
It's not going to be exact, but you can count the number of bytes in a string to get a rough estimation.
function bytes(string) {
var escaped_string = encodeURI(string);
if (escaped_string.indexOf("%") != -1) {
var count = escaped_string.split("%").length - 1;
count = count == 0 ? 1 : count;
count = count + (escaped_string.length - (count * 3));
}
else {
count = escaped_string.length;
}
return count;
}
var mystring = 'tâ';
alert(bytes(mystring));
If you are talking about memory usage, then no. There is no way of reliably determining the used memory (at least implementation-independently), since this is not part of the ECMAScript spec. It depends on your character encoding.
It depends on the data in your string and the way it is stored. If your Base64 encoded string is stored as a Base64 encoded string, the length is the same as the size on disk. If not, you have to decode it
I found a solution (although it seems a bit icky) here
function checkLength() {
var countMe = document.getElementById("someText").value
var escapedStr = encodeURI(countMe)
if (escapedStr.indexOf("%") != -1) {
var count = escapedStr.split("%").length - 1
if (count == 0) count++ //perverse case; can't happen with real UTF-8
var tmp = escapedStr.length - (count * 3)
count = count + tmp
} else {
count = escapedStr.length
}
alert(escapedStr + ": size is " + count)
}
You can count the number of bytes in a string by this simple and precise way
var head = 'data:image/png;base64,';
var imgFileSize = Math.round((string.length - head.length)*3/4) ;
console.log("size is ",imgFileSize);
本文标签: How can I estimate the disk size of a string with JavaScriptStack Overflow
版权声明:本文标题:How can I estimate the disk size of a string with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741220835a2360939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论