Calculate string byte size

Benchmark created on


Description

Compares calculation of byte size from a string

Setup

function randomString(minLen = 1, maxLen = 5000, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
  const len = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen; // length in [1,5000]
  const out = new Array(len);
  const n = chars.length;
  for (let i = 0; i < len; i++) out[i] = chars[Math.floor(Math.random() * n)];
  return out.join('');
}
const body = randomString();

Test runner

Ready to run.

Testing in
TestOps/sec
new TextEncoder().encode().length
const bytes = new TextEncoder().encode(body).length;
ready
new Blob().size
const bytes = new Blob([body]).size
ready

Revisions

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