before ?

Benchmark created on


Setup

const inputs = [
  "example.com?query=string",
  "example.com?another=query",
  "example.com",
  "example.com?yet=another&query=string"
];

function methodSplit(input) {
  return input.split("?")[0];
}

// Method 2: Using String.indexOf() and String.slice()
function methodIndexOfSlice(input) {
  const index = input.indexOf("?");
  return index === -1 ? input : input.slice(0, index);
}

// Method 3: Using Regular Expressions
function methodRegex(input) {
  return input.match(/^[^?]*/)[0];
}

Test runner

Ready to run.

Testing in
TestOps/sec
split
const x = inputs.forEach(methodSplit);
ready
indexof + slice
const x = inputs.forEach(methodIndexOfSlice);
ready
regex
const x = inputs.forEach(methodRegex);
ready

Revisions

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