decodetext (v2)

Revision 2 of this benchmark created on


Setup

const u8a = new TextEncoder().encode('日本国')

const decoder = new TextDecoder()

Test runner

Ready to run.

Testing in
TestOps/sec
via other APIs (threejs)
let s = '';

		for ( let i = 0, il = u8a.length; i < il; i ++ ) {

			// Implicitly assumes little-endian.
			s += String.fromCharCode( u8a[ i ] );

		}



			// merges multi-byte utf-8 characters.

			decodeURIComponent( escape( s ) );


ready
via manually
    let s = ''
    for (let i = 0, l = u8a.length; i < l;) {
        if (u8a[i] >> 7 === 0) {
            s += String.fromCodePoint(u8a[i])
            i += 1
            continue
        } else if (u8a[i] >> 5 === 0b110) {
            if (u8a[i + 1] >> 6 === 0b10) {
                s += String.fromCodePoint(((u8a[i] & 0b11111) << 6) + (u8a[i + 1] & 0b111111)) ;
                i += 2
                continue
            }
        } else if (u8a[i] >> 4 === 0b1110) {
            if (u8a[i + 1] >> 6 === 0b10 && u8a[i + 2] >> 6 === 0b10) {
                s += String.fromCodePoint(((u8a[i] & 0b1111) << 12) + ((u8a[i + 1] & 0b111111) << 6) + (u8a[i + 2] & 0b111111))
                i += 3
                continue
            }
        } else if (u8a[i] >> 3 === 0b11110) {
            if (u8a[i + 1] >> 6 === 0b10 && u8a[i + 2] >> 6 === 0b10 && u8a[i + 3] >> 6 === 0b10) {
                s += String.fromCodePoint(((u8a[i] & 0b111) << 18) + ((u8a[i + 1] & 0b111111) << 12) + ((u8a[i + 2] & 0b111111) << 6) + (u8a[i + 3] & 0b111111))
                i += 4
                continue
            }
        } else if (u8a[i] >> 2 === 0b111110) {
            if (u8a[i + 1] >> 6 === 0b10 && u8a[i + 2] >> 6 === 0b10 && u8a[i + 3] >> 6 === 0b10 && u8a[i + 4] >> 6 === 0b10) {
                s += String.fromCodePoint(((u8a[i] & 0b11) << 24) + ((u8a[i + 1] & 0b111111) << 18) + ((u8a[i + 2] & 0b111111) << 12) + ((u8a[i + 3] & 0b111111) << 6) + (u8a[i + 4] & 0b111111))
                i += 5
                continue
            }
        } else if (u8a[i] >> 1 === 0b1111110) {
            if (u8a[i + 1] >> 6 === 0b10 && u8a[i + 2] >> 6 === 0b10 && u8a[i + 3] >> 6 === 0b10 && u8a[i + 4] >> 6 === 0b10 && u8a[i + 5] >> 6 === 0b10) {
                s += String.fromCodePoint(((u8a[i] & 0b1) << 30) + ((u8a[i + 1] & 0b111111) << 24) + ((u8a[i + 2] & 0b111111) << 18) + ((u8a[i + 3] & 0b111111) << 12) + ((u8a[i + 4] & 0b111111) << 6) + (u8a[i + 5] & 0b111111))
                i += 6
                continue
            }
        }
        s += 0xfffd
        i += 1
    }
ready
via TextDecoder
decoder.decode(u8a)
ready

Revisions

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