URL Construction

Benchmark created on


Setup

const base = 'https://www.example.com';
const params = {
	foo: 'bar',
	baz: true,
};

Test runner

Ready to run.

Testing in
TestOps/sec
Setting URL search
const endpoint = new URL(base);
endpoint.search = new URLSearchParams(params).toString();
ready
URL with relative path
const endpoint = new URL('?' + new URLSearchParams(params).toString(), base);
ready
Array join
const res = [];
for (let key in params) {
  res.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
}
const endpoint = res.join("&");
ready
Template literal URLSearchParams
const endpoint = `${base}?${new URLSearchParams(params).toString()}`;
ready
Object.entries map
const endpoint = base + '?' + Object.entries(params)
  .map((pair) => pair.map(encodeURIComponent).join("="))
  .join("&");
ready

Revisions

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