Created
September 25, 2013 20:46
-
-
Save jussimalinen/6705787 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="output"></div> | |
<script type="text/javascript"> | |
String.prototype.repeat = function(num) { | |
return new Array(isNaN(num)? 1 : ++num).join(this); | |
} | |
var output = document.getElementById('output'); | |
var shortStr = '\xe8\x87\xaa\xe8\x8d\x8a\xe6\xb9\x96\xe4\xbb\xa5\xe5\x8c\x97'; | |
var longStr = shortStr.repeat(1000); | |
var newDecode = function (utftext) { | |
return decodeURIComponent(escape(utftext)); | |
}; | |
var oldDecode = function (utftext) { | |
var string = [], | |
i = 0, | |
c = 0, | |
c2 = 0, | |
c3 = 0, | |
len = utftext.length; | |
while (i < len) { | |
c = utftext.charCodeAt(i); | |
if (c < 128) { | |
string.push(String.fromCharCode(c)); | |
i++; | |
} else if ((c > 191) && (c < 224)) { | |
c2 = utftext.charCodeAt(i + 1); | |
string.push(String.fromCharCode(((c & 31) << 6) | (c2 & 63))); | |
i += 2; | |
} else { | |
c2 = utftext.charCodeAt(i + 1); | |
c3 = utftext.charCodeAt(i + 2); | |
string.push(String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))); | |
i += 3; | |
} | |
} | |
return string.join(''); | |
} | |
var before = new Date().getTime() | |
oldDecode(longStr); | |
var oldTime = new Date().getTime() - before; | |
before = new Date().getTime() | |
newDecode(longStr); | |
var newTime = new Date().getTime() - before; | |
output.innerHTML += 'Old: ' + oldTime.toString() + 'ms<br />'; | |
output.innerHTML += 'New: ' + newTime.toString() + 'ms'; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment