Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Convert binary string to bytes
To use browser's atob() or btoa(), we need to convert between bytes and so-called "binary" string. This test converts "binary" string to bytes, for working with results from atob() calls, which decodes base64 to bytes (in the form of binary string).
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser:
Chrome 133
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Uint8Array.from + codePointAt
13.0 Ops/sec
Uint8Array.from + charCodeAt
13.2 Ops/sec
for-loop + codePointAt
9.9 Ops/sec
for-loop + charCodeAt
7.2 Ops/sec
Script Preparation code:
function get1024Bytes(n) { const bytes = new Uint8Array(n * 1024); for (let i = 0; i < bytes.length; i += 1) { bytes[i] = Math.random() * 0xff; } return bytes; } const bytes = get1024Bytes(1024); /* Note that Function.prototype.apply() has a limit, so not using it to creat binary string here for convenience. To use it, split the array into smaller chunks first. String.fromCodePoint.apply(null, bytes); // bytes.length should not exceed the maximum See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply */ const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join("");
Tests:
Uint8Array.from + codePointAt
return Uint8Array.from(binString, (m) => m.codePointAt(0));
Uint8Array.from + charCodeAt
return Uint8Array.from(binString, (m) => m.charCodeAt(0));
for-loop + codePointAt
const bytes = new Uint8Array(binString.length); for (let i = 0; i < bytes.length; i += 1) { bytes[i] = binString[i].codePointAt(0); } return bytes;
for-loop + charCodeAt
const bytes = new Uint8Array(binString.length); for (let i = 0; i < bytes.length; i += 1) { bytes[i] = binString[i].charCodeAt(0); } return bytes;
for-loop + Uint8Array.from + codePointAt (small chunk)
const QUANTUM = 128; const bytes = new Uint8Array(binString.length); for (let i = 0; i < binString.length; i += QUANTUM) { let j = i; for (const b of Uint8Array.from(binString.slice(i, i + QUANTUM), (m) => m.codePointAt(0))) { bytes[j] = b; j += 1; } } return bytes;
for-loop + Uint8Array.from + charCodeAt (small chunk)
const QUANTUM = 128; const bytes = new Uint8Array(binString.length); for (let i = 0; i < binString.length; i += QUANTUM) { let j = i; for (const b of Uint8Array.from(binString.slice(i, i + QUANTUM), (m) => m.charCodeAt(0))) { bytes[j] = b; j += 1; } } return bytes;
for-loop + Uint8Array.from + codePointAt (big chunk)
const QUANTUM = 32768; const bytes = new Uint8Array(binString.length); for (let i = 0; i < binString.length; i += QUANTUM) { let j = i; for (const b of Uint8Array.from(binString.slice(i, i + QUANTUM), (m) => m.codePointAt(0))) { bytes[j] = b; j += 1; } } return bytes;
for-loop + Uint8Array.from + charCodeAt (big chunk)
const QUANTUM = 32768; const bytes = new Uint8Array(binString.length); for (let i = 0; i < binString.length; i += QUANTUM) { let j = i; for (const b of Uint8Array.from(binString.slice(i, i + QUANTUM), (m) => m.charCodeAt(0))) { bytes[j] = b; j += 1; } } return bytes;