tc39 / ecmarkup

An HTML superset/Markdown subset source format for ECMAScript and related specifications
https://tc39.es/ecmarkup/
MIT License
221 stars 63 forks source link

avoid iterating a live nodelist #588

Closed bakkot closed 8 months ago

bakkot commented 8 months ago

This step is still surprisingly slow but this makes it take 5 seconds instead of 10 on my machine.

gibson042 commented 8 months ago

It occurs to me that even more time could be saved by skipping this step entirely and instead updating the print CSS to use relevant attribute-value-starts-with selectors:

+/* Render non-mailto external link targets, cf. https://url.spec.whatwg.org/#special-scheme */
-a[data-print-href]::after {
+a:is([href^='ftp:' i], [href^='sftp:' i], [href^='http:' i], [href^='https:' i], [href^='ws:' i], [href^='wss:' i])::after {
   content: ' <' attr(href) '>';
   color: initial;
 }

This would miss new schemes until added to the CSS (which is lintable) and would be redundant for links whose text matches their target (which we should probably not have anyway—it's more accessible to name links and let user agents handle URIs).

ljharb commented 8 months ago

also if you're going for speed, you should avoid for..of aggressively.

michaelficarra commented 8 months ago

and would be redundant for links whose text matches their target (which we should probably not have anyway—it's more accessible to name links and let user agents handle URIs)

Yes but we do and that's why we can't use this CSS-only strategy.

also if you're going for speed, you should avoid for..of aggressively.

The for-of is only iterating 94 elements. It's not the slow part. The slow part, for some unexplainable reason, is setting an attribute.

bakkot commented 8 months ago

The for-of is only iterating 94 elements.

(Well, no, it's iterating 30k elements, but it's still true that the iteration itself is now ~instant.)

gibson042 commented 8 months ago

and would be redundant for links whose text matches their target (which we should probably not have anyway—it's more accessible to name links and let user agents handle URIs)

Yes but we do and that's why we can't use this CSS-only strategy.

Having them doesn't preclude a CSS-only approach, it just means that such an approach would duplicate URLs. But to address the larger point, would you object to fixing such links by giving them non-URL text?

michaelficarra commented 8 months ago

@gibson042 You're always welcome to send a PR to 262, but (without looking) I doubt you'll completely eliminate them.

gibson042 commented 8 months ago

ecma262 PR: https://github.com/tc39/ecma262/pull/3301