Skip to content

Instantly share code, notes, and snippets.

@lirongfei123
Created September 20, 2016 03:59
Show Gist options
  • Save lirongfei123/fa52d17b9351ef95937ce69401f1ee49 to your computer and use it in GitHub Desktop.
Save lirongfei123/fa52d17b9351ef95937ce69401f1ee49 to your computer and use it in GitHub Desktop.
function getStringMemorySize(_string) {
"use strict";
var codePoint, accum = 0;
for (var stringIndex = 0, endOfString = _string.length; stringIndex < endOfString; stringIndex++) {
codePoint = _string.charCodeAt(stringIndex);
if (codePoint < 0x100) {
accum += 1;
continue;
}
if (codePoint < 0x10000) {
accum += 2;
continue;
}
if (codePoint < 0x1000000) {
accum += 3;
} else {
accum += 4;
}
}
return accum * 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment