test unicode to utf8

JavaScript performance comparison

Test case created by ukyo

Preparation code

<script>
function unicode2utf8_array(str){
    var n = str.length,
        idx = -1,
        bytes = [],
        i, j, c;
    
    for(i = 0; i < n; ++i){
        c = str.charCodeAt(i);
        if(c <= 0x7F){
            bytes[++idx] = c;
        } else if(c <= 0x7FF){
            bytes[++idx] = 0xC0 | (c >>> 6);
            bytes[++idx] = 0x80 | (c & 0x3F);
        } else if(c <= 0xFFFF){
            bytes[++idx] = 0xE0 | (c >>> 12);
            bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
            bytes[++idx] = 0x80 | (c & 0x3F);
        } else {
            bytes[++idx] = 0xF0 | (c >>> 18);
            bytes[++idx] = 0x80 | ((c >>> 12) & 0x3F);
            bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
            bytes[++idx] = 0x80 | (c & 0x3F);
        }
    }
    return bytes;
}

function unicode2utf8_uint8array(str){
    var n = str.length,
        idx = -1,
        byteLength = 512,
        bytes = new Uint8Array(byteLength),
        i, c, _bytes;
    
    for(i = 0; i < n; ++i){
        c = str.charCodeAt(i);
        if(c <= 0x7F){
            bytes[++idx] = c;
        } else if(c <= 0x7FF){
            bytes[++idx] = 0xC0 | (c >>> 6);
            bytes[++idx] = 0x80 | (c & 0x3F);
        } else if(c <= 0xFFFF){
            bytes[++idx] = 0xE0 | (c >>> 12);
            bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
            bytes[++idx] = 0x80 | (c & 0x3F);
        } else {
            bytes[++idx] = 0xF0 | (c >>> 18);
            bytes[++idx] = 0x80 | ((c >>> 12) & 0x3F);
            bytes[++idx] = 0x80 | ((c >>> 6) & 0x3F);
            bytes[++idx] = 0x80 | (c & 0x3F);
        }
        if(byteLength - idx <= 4){
            _bytes = bytes;
            byteLength *= 2;
            bytes = new Uint8Array(byteLength);
            bytes.set(_bytes);
        }
    }
    return bytes.subarray(0, ++idx);
}

function unicode2utf8_filereader(str, callback) {
  var fr = new FileReader();
  fr.onloadend = function() {
    callback(new Uint8Array(fr.result));
  };
  fr.readAsArrayBuffer(new Blob([str]));
}

var text = '';
text += 'abcdefghijklmnopqrstuvwxyz';
text += 'あいうえおかきくけこさしすせそたちつてと';
text += 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒ';
text += '☺☻✌☹♡♥❤⚘❀❃❁✼☀✌♫♪☃❄❅❆';
</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
Array
var result;
result = unicode2utf8_array(text);
pending…
Uint8Array
var result;
result = unicode2utf8_uint8array(text);
pending…
FileReader
// async test
var result;
unicode2utf8_filereader(text, function(res){
    result = res;
    deferred.resolve();
});
pending…

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

Compare results of other browsers

0 Comments