Text(En/De)coder vs (de/en)codeURIComponent +- (un)escape

Benchmark created on


Setup

function base64ToBytes(base64) {
  const binString = atob(base64);
  return Uint8Array.from(binString, (m) => m.codePointAt(0));
}

function decode_utf8( s ) {
  return decodeURIComponent( escape( atob(s) ) );
}

function decode_utf8_noescape( s ) {
  return decodeURIComponent(atob(s) );
}

///////

function bytesToBase64(bytes) {
  const binString = Array.from(bytes, (byte) =>
    String.fromCodePoint(byte),
  ).join("");
  return btoa(binString);
}

function encode_utf8( s ) {
  return btoa(unescape( encodeURIComponent( s ) ));
}

function encode_utf8_nounescape( s ) {
  return btoa(encodeURIComponent( s ));
}

///////

let str = 'a Δ€ 𐀀 ζ–‡ δΈ­ πŸ¦„'
while (str.length < 1_000_000) {
	str = str.concat(str);
}


Test runner

Ready to run.

Testing in
TestOps/sec
Text(En/De)coder
const b64 = bytesToBase64(new TextEncoder().encode(str));
const decoded = new TextDecoder().decode(base64ToBytes(b64));
ready
(de/en)codeURIComponent + (un)escape
const b64 = encode_utf8(str);
const decoded = decode_utf8(b64)
ready
(de/en)codeURIComponent
const b64 = encode_utf8_nounescape(str);
const decoded = decode_utf8_noescape(b64)
ready

Revisions

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