test utf8 to unicode

JavaScript performance comparison

Test case created by ukyo

Preparation code

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

var text = '';
for(var i = 0; i < 10000; i++){
    text += 'abcdefghijklmnopqrstuvwxyz';
    text += 'あいうえおかきくけこさしすせそたちつてと';
    text += 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒ';
    text += '☺☻✌☹♡♥❤⚘❀❃❁✼☀✌♫♪☃❄❅❆';
}

var bytes;
var blob = new Blob([text]);
var fr = new FileReader();
fr.onloadend = function() { bytes = new Uint8Array(fr.result);};
fr.readAsArrayBuffer(blob);
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
test1
bytesToString(bytes);
pending…
test2
bytesToString2(bytes);
pending…
filereader1
// async test
var fr = new FileReader();
fr.onloadend = function(){deferred.resolve();};
fr.readAsText(blob);
pending…
filereader2
// async test
var fr = new FileReader();
fr.onloadend = function(){deferred.resolve();};
fr.readAsText(new Blob([bytes]));
pending…
legacy
bytesToStringLegacy(bytes);
pending…
test3
bytesToString(bytes, 0x8000);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments