bytes of utf8 to unicode速度比較

TypedArray#subarrayはコピーが発生しないのでとにかく速い!今後、効率的な実装を行うときの強力な武器になりそう。まぁ、今回の場合はFileReader#readAsTextという万能の願望器があるので素直にそれを使ったほうが良さそう(特にセキュリティ的なこと含めると)。

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;
};



posted by 右京 | javascript
blog comments powered by Disqus
×

この広告は1年以上新しい記事の投稿がないブログに表示されております。