base64 vs url encode

Benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/buffer/6.0.3/buffer.min.js" />

Setup

// Generate a ~1KB JSON string
const largeObject = {
  id: "bench-test-999",
  timestamp: new Date().toISOString(),
  description: "Performance comparison for encoding methods",
  // Fill with ~900 bytes of data to reach ~1KB total JSON size
  payload: "x".repeat(900),
  tags: ["perf", "encoding", "btoa", "buffer", "urlencode"]
};

const jsonString = JSON.stringify(largeObject);

// Pre-encoded versions for the decoding (vice-versa) tests
const b64Data = btoa(jsonString);
const urlData = encodeURIComponent(jsonString);
const bufferData = Buffer.from(jsonString).toString('base64');

Test runner

Ready to run.

Testing in
TestOps/sec
btoa
const res = btoa(jsonString);
ready
encodeURIComponent
const res = encodeURIComponent(jsonString);
ready
Buffer.from
onst res = Buffer.from(jsonString).toString('base64');
ready
atob
const res = atob(b64Data);
ready
decodeURIComponent
const res = decodeURIComponent(urlData);
ready
Buffer.from
const res = Buffer.from(bufferData, 'base64').toString();
ready

Revisions

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