Created
October 16, 2016 11:04
-
-
Save DarkPark/3a03c0751d57c4e64d228b8e8126eefa to your computer and use it in GitHub Desktop.
manual conversion vs TextEncoder
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
var tries = 100000, | |
encoder = new TextEncoder('utf-8'), | |
example = 'I♥☢𝄢. I\'m a ☢ ☃ that plays 𝄢 guitar and spea̧͈͖ks Ar̽̾̈́͒͑ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜!', | |
time, index; | |
function utf8ToBinaryString ( str ) { | |
var escstr = encodeURIComponent(str); | |
// replaces any uri escape sequence, such as %0A, | |
// with binary escape, such as 0x0A | |
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function ( match, p1 ) { | |
return String.fromCharCode(parseInt(p1, 16)); | |
}); | |
return binstr; | |
} | |
function binaryStringToBuffer ( binstr ) { | |
var buf; | |
if ( 'undefined' !== typeof Uint8Array ) { | |
buf = new Uint8Array(binstr.length); | |
} else { | |
buf = []; | |
} | |
Array.prototype.forEach.call(binstr, function ( ch, i ) { | |
buf[i] = ch.charCodeAt(0); | |
}); | |
return buf; | |
} | |
function utf8ToBuffer ( str ) { | |
var binstr = utf8ToBinaryString(str); | |
var buf = binaryStringToBuffer(binstr); | |
return buf; | |
} | |
console.log('match conversions: %s', encoder.encode(example).join(',') === utf8ToBuffer(example).join(',')); | |
time = Date.now(); | |
for ( index = 0; index < tries; index++ ) { | |
encoder.encode(example); | |
} | |
console.log('TextEncoder is ready in %sms', Date.now() - time); | |
time = Date.now(); | |
for ( index = 0; index < tries; index++ ) { | |
utf8ToBuffer(example); | |
} | |
console.log('manual conversion is ready in %sms', Date.now() - time); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment