Editing Encode/Decode String This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer lectus quam, adipiscing sit amet lobortis eu, lacinia ac lacus. Etiam luctus commodo enim, sit amet iaculis ante dictum in. Ut ut eros sed lacus eleifend pretium. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum vel elit neque. Etiam fringilla lobortis arcu et sollicitudin. Nulla convallis augue at sapien tempor scelerisque. Donec felis mi, tempor sit amet aliquet et, laoreet id elit. Nunc mollis, arcu sed laoreet fermentum, ligula urna fringilla enim, vel dictum massa massa vel nibh. Pellentesque vel mauris massa. Phasellus vulputate nisl sit amet velit bibendum ac commodo quam interdum. Quisque sed enim neque, vitae faucibus augue. Nulla iaculis arcu a ligula congue convallis. Suspendisse bibendum tincidunt elit quis luctus. Donec consequat viverra urna in lacinia. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nunc sit amet."; function encode_utf8(data) { if (data == '' || data == null) { return ''; } var buffer = []; var pos = 0; for (var i = 0; i < data.length; i++) { var c = data.charCodeAt(i); if (c > 0x10000) { // 4 bytes buffer[pos++] = 0xF0 | ((c & 0x1C0000) >>> 18); buffer[pos++] = 0x80 | ((c & 0x3F000) >>> 12); buffer[pos++] = 0x80 | ((c & 0xFC0) >>> 6); buffer[pos++] = 0x80 | (c & 0x3F); } else if (c > 0x800) { // 3 bytes buffer[pos++] = 0xE0 | ((c & 0xF000) >>> 12); buffer[pos++] = 0x80 | ((c & 0xFC0) >>> 6); buffer[pos++] = 0x80 | (c & 0x3F); } else if (c > 0x80) { // 2 bytes buffer[pos++] = 0xC0 | ((c & 0x7C0) >>> 6); buffer[pos++] = 0x80 | (c & 0x3F); } else { // 1 byte buffer[pos++] = c; } } return new Uint8Array(buffer); } function decode_utf8(bytes) { var i = 0, str = '', size = bytes.length, c, code; while (i < size) { c = bytes[i]; if (c < 128) { str += String.fromCharCode(c); i++; } else if ((c ^ 0xc0) < 32) { code = ((c ^ 0xc0) << 6) | (bytes[i + 1] & 63); str += String.fromCharCode(code); i += 2; } else { code = ((c & 15) << 12) | ((bytes[i + 1] & 63) << 6) | (bytes[i + 2] & 63); str += String.fromCharCode(code); i += 3; } } return str; } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code decode_utf8(encode_utf8(input)) Test 2 Title Async (check if this is an asynchronous test) Code var blob = new Blob([input]); var reader = new FileReader(); reader.onload = function(evt) { deferred.resolve(); }; reader.readAsText(blob); Test 3 Title Async (check if this is an asynchronous test) Code var blob = new Blob([input]); var reader = new FileReader(); reader.onload = function(evt) { decode_utf8(evt.target.result); deferred.resolve(); }; reader.readAsArrayBuffer(blob);