Created
September 20, 2016 03:59
-
-
Save lirongfei123/fa52d17b9351ef95937ce69401f1ee49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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