http://jsperf.com/test-utf8-to-unicode
bytesToString = function(bytes, bufferSize){
bufferSize = bufferSize || 8192
var buffer = new Uint32Array(bufferSize),
n = bytes.length,
i = 0,
result = '',
j, head;
while(i < n) {
for(j = 0; j < bufferSize && i < n; ++i, ++j) {
head = bytes[i];
if((head >>> 7) === 0) {
buffer[j] = head;
} else if((head >>> 5) === 0x05) {
buffer[j] = ((head & 0x1F) << 6) | (bytes[++i] & 0x3F);
} else if((head >>> 4) === 0x0E) {
buffer[j] =
((head & 0x0F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
} else {
buffer[j] =
((head & 0x07) << 20) |
((bytes[++i] & 0x3F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
}
}
result += String.fromCharCode.apply(void 0, buffer.subarray(0, j));
}
return result;
};
bytesToString2 = function(bytes, bufferSize){
bufferSize = bufferSize || 8192
var buffer = new Uint32Array(bufferSize),
n = bytes.length,
i = 0,
arr = [],
j, head;
while(i < n) {
for(j = 0; j < bufferSize && i < n; ++i, ++j) {
head = bytes[i];
if((head >>> 7) === 0) {
buffer[j] = head;
} else if((head >>> 5) === 0x05) {
buffer[j] = ((head & 0x1F) << 6) | (bytes[++i] & 0x3F);
} else if((head >>> 4) === 0x0E) {
buffer[j] =
((head & 0x0F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
} else {
buffer[j] =
((head & 0x07) << 20) |
((bytes[++i] & 0x3F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
}
}
arr[arr.length] = String.fromCharCode.apply(void 0, buffer.subarray(0, j));
}
return arr.join('');
};
bytesToStringLegacy = function(bytes){
var n = bytes.length,
i = 0,
result = '',
j, head, c;
while(i < n) {
head = bytes[i];
if((head >>> 7) === 0) {
c = head;
} else if((head >>> 5) === 0x05) {
c = ((head & 0x1F) << 6) | (bytes[++i] & 0x3F);
} else if((head >>> 4) === 0x0E) {
c =
((head & 0x0F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
} else {
c =
((head & 0x07) << 20) |
((bytes[++i] & 0x3F) << 12) |
((bytes[++i] & 0x3F) << 6) |
(bytes[++i] & 0x3F);
}
result += String.fromCharCode(c);
++i;
}
return result;
};