addHttps (v2)

Revision 2 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
regexp
function getUrlWithHttps(url) {
  if (!/^(http|https):\/\//.test(url)) {
  	return `https://${url}`
  }

  return url
}

getUrlWithHttps('http://s.com/')
getUrlWithHttps('https://s.com/')
getUrlWithHttps('www.s.com')
getUrlWithHttps()
ready
js
function getUrlWithHttps(url) {
  if (url && !url.startsWith('http://') && !url.startsWith('https://')) {
  	return `https://${url}`
  }

  return url
}

getUrlWithHttps('http://s.com/')
getUrlWithHttps('https://s.com/')
getUrlWithHttps('www.s.com')
getUrlWithHttps()
ready
regexp shorter
function getUrlWithHttps(url) {
  if (!/^https?:/.test(url)) {
  	return `https://${url}`
  }

  return url
}

getUrlWithHttps('http://s.com/')
getUrlWithHttps('https://s.com/')
getUrlWithHttps('www.s.com')
getUrlWithHttps()
ready

Revisions

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