Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src_url_equal is slow when dealing with large base64 encoded images #8164

Closed
EmilTholin opened this issue Jan 4, 2023 · 3 comments
Closed

Comments

@EmilTholin
Copy link
Member

Describe the bug

This discussion on Discord made us realize that trying to remove an element from an each block containing images with large base64 encoded images takes a long time.

This is because the src_url_equal check in the generated code is slow for these types of images.

if (dirty & /*images*/ 1 && !src_url_equal(img.src, img_src_value = /*image*/ ctx[3].src)) {
	attr(img, "src", img_src_value);
}

let src_url_equal_anchor;
export function src_url_equal(element_src, url) {
if (!src_url_equal_anchor) {
src_url_equal_anchor = document.createElement('a');
}
src_url_equal_anchor.href = url;
return element_src === src_url_equal_anchor.href;
}

Reproduction

REPL

Logs

No response

System Info

REPL, v3.55.0

Severity

annoyance

@Conduitry
Copy link
Member

I don't remember why we were doing it this way. To support older browsers without the URL global I'm guessing? Do you get the same behavior if you compare element_src === new URL(url, document.location).href (and is that appreciably faster)? Maybe we could have two versions of this code and use the URL global if it's available.

@dominikg
Copy link
Member

dominikg commented Jan 4, 2023

we should try to avoid doing any assignments that are not needed in this case. If this is only relevant for relative/absolute conversion, testing that url is indeed relative and does not start with a protocol would be the first thing that comes to my mind to prevent this. Using URL instead of a is just a slight difference in implementation, which is slightly slower according to this comment: #6449 (comment)

@EmilTholin
Copy link
Member Author

I experimented with an overly-simplistic src_url_equal, and the difference in speed was almost negligible.

export function src_url_equal(element_src, url) {
+	return element_src === url;
-	if (!src_url_equal_anchor) { 
-		src_url_equal_anchor = document.createElement('a'); 
-	} 
-	src_url_equal_anchor.href = url; 
-	return element_src === src_url_equal_anchor.href; 
} 

I guess comparing all these long strings against each other is slow no matter how you do it.

It is really fast in the Svelte 5 preview REPL though (I can't share it because the base64 image is too big for the URL) because of the fine-grained array updates, so this will not be an issue going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants