Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Micro optimize URL
  • Loading branch information
Jarred-Sumner committed Apr 10, 2024
commit 420e1f3bb84c6f98b47dde84633a7c9bf5232477
7 changes: 7 additions & 0 deletions bench/snippets/url.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bench, run } from './runner.mjs'

bench('url', () => {
const url = new URL('https://example.com/')
})

await run()
10 changes: 9 additions & 1 deletion src/bun.js/bindings/DOMURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace WebCore {

static inline String redact(const String& input)
{
if (input.contains("@"_s))
if (input.contains('@'))
return "<redacted>"_s;

return makeString('"', input, '"');
Expand All @@ -61,6 +61,14 @@ inline DOMURL::DOMURL(URL&& completeURL)
ASSERT(m_url.isValid());
}

ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url)
{
URL completeURL { url };
if (!completeURL.isValid())
return Exception { TypeError, makeString(redact(url), " cannot be parsed as a URL.") };
return adoptRef(*new DOMURL(WTFMove(completeURL)));
}

ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
{
ASSERT(base.isValid() || base.isNull());
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/DOMURL.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class URLSearchParams;
class DOMURL final : public RefCounted<DOMURL>, public CanMakeWeakPtr<DOMURL>, public URLDecomposition {
public:
static ExceptionOr<Ref<DOMURL>> create(const String& url, const String& base);
static ExceptionOr<Ref<DOMURL>> create(const String& url);
WEBCORE_EXPORT ~DOMURL();

static RefPtr<DOMURL> parse(const String& url, const String& base);
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/webcore/JSDOMURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ template<> EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSDOMURLDOMConstructor::const
EnsureStillAliveScope argument1 = callFrame->argument(1);
auto base = argument1.value().isUndefined() ? String() : convert<IDLUSVString>(*lexicalGlobalObject, argument1.value());
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
auto object = DOMURL::create(WTFMove(url), WTFMove(base));
auto object = base.isEmpty() ? DOMURL::create(WTFMove(url)) : DOMURL::create(WTFMove(url), WTFMove(base));
if constexpr (IsExceptionOr<decltype(object)>)
RETURN_IF_EXCEPTION(throwScope, {});
static_assert(TypeOrExceptionOrUnderlyingType<decltype(object)>::isRef);
Expand Down