own-impl-vs-url

Benchmark created on


Setup

function parseURL(exampleUrl) {
  const params = {};

  let host;
  let port = 80;
  let pathname;
  let protocol;
  let query;

  return (url) => {
    let pointer = 0;

    let prev;
    let pprev;

    let i = 0;
    while (i < url.length) {
      const char = url.charAt(i);

      if (pointer === 0 && char === '/' && prev === '/' && pprev === ':') {
        protocol = url.substring(0, i - 2);
        pointer = i;
      } else if (pointer > 0 && protocol && !host && char === '/') {
        host = url.substring(pointer + 1, i);
        pointer = i;
      } else if (pointer > 0 && protocol && !host && char === ':') {
        host = url.substring(pointer + 1, i);
        pointer = i;
      } else if (pointer > 0 && protocol && host && char === '/') {
        port = (url.substring(pointer + 1, i));
        pointer = i;
      } else if (pointer > 0 && protocol && host && port) {
        pathname = url.substring(pointer + 1, i + 1);
      } else if (pointer > 0 && protocol && host && port && pathname) {
        query = '';
      }

      pprev = prev;
      prev = char;
      i++
    }

    return {
      href: url,
      origin: `${protocol}://${host}:${port}`,
      protocol,
      host: `${host}:${port}`,
      hostname: host,
      port,
      pathname
    }
  }
}
const prepareMatches = (match) => {
  let i = 0;
  let lastIndex = -1;
  const map = [];

  let currentPath;
  let char;

  while ((i = match.indexOf('/', lastIndex)) !== -1) {
    currentPath = match.substring(lastIndex, i);
    char = currentPath.charAt(0);

    if (char === ':') {
      map.push(currentPath.substr(1));
    } else {
      map.push(null);
    }

    lastIndex = i + 1;
  }

  currentPath = match.substring(lastIndex);
  char = currentPath.charAt(0);

  if (char === ':') {
    map.push(currentPath.substr(1));
  } else {
    map.push(null);
  }

  return map;
};
const fastPathParse = (match) => {
  const map = prepareMatches(match);

  // eslint-disable-next-line complexity
  return (path) => {
    let params = null;

    let i;
    let lastIndex = -1;
    let index = 0;

    let key;
    let value;

    while ((i = path.indexOf('/', lastIndex)) !== -1) {
      value = path.substring(lastIndex, i);

      key = map[index];
      if (key) {
        if (!params) {
          params = {};
        }

        params[key] = value;
      }

      lastIndex = i + 1;
      index += 1;
    }

    value = path.substring(lastIndex);

    key = map[index];
    if (key) {
      if (!params) {
        params = {};
      }

      params[key] = value;
    }

    return params;
  };
};

const URL1 = parseURL(`/users/:id/settings`);
const URL2 = fastPathParse(`/users/:id/settings`);

Test runner

Ready to run.

Testing in
TestOps/sec
own-impl
URL1(`http://localhost:5001/users/3003/settings`)
ready
url
new URL(`http://localhost:5001/users/3003/settings`)
ready
fast-path-parse
URL2(`http://localhost:5001/users/3003/settings`)
ready

Revisions

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