Skip to content

Instantly share code, notes, and snippets.

@incompl
Created September 20, 2012 18:43
Show Gist options
  • Save incompl/3757612 to your computer and use it in GitHub Desktop.
Save incompl/3757612 to your computer and use it in GitHub Desktop.
var msg = JSON.stringify( state );
var getUTF8Size = function( str ) {
var sizeInBytes = str.split('')
.map(function( ch ) {
return ch.charCodeAt(0);
}).map(function( uchar ) {
// The reason for this is explained later in
// the section “An Aside on Text Encodings”
return uchar < 128 ? 1 : 2;
}).reduce(function( curr, next ) {
return curr + next;
});
return sizeInBytes;
};
var msgSize = getUTF8Size( msg ); // 410 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment