diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php
index 83c1784418248..0cdcd4cd85531 100644
--- a/src/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -1496,13 +1496,48 @@ private function skip_script_data(): bool {
while ( false !== $at && $at < $doc_length ) {
$at += strcspn( $html, '-<', $at );
+ /*
+ * Optimization: Terminating a complete script element requires at least eight
+ * additional bytes in the document. Some checks below may cause local escaped
+ * state transitions when processing shorter strings, but those transitions are
+ * irrelevant if the script tag is incomplete and the function must return false.
+ *
+ * This may need updating if those transitions become significant or exported from
+ * this function in some way, such as when building safe methods to embed JavaScript
+ * or data inside a SCRIPT element.
+ *
+ * $at may be here.
+ * ↓
+ * ...
+ * ╰──┬───╯
+ * $at + 8 additional bytes are required for a non-false return value.
+ *
+ * This single check eliminates the need to check lengths for the shorter spans:
+ *
+ * $at may be here.
+ * ↓
+ *
+ * ├╯
+ * $at + 2 additional characters does not require a length check.
+ *
+ * The transition from "escaped" to "unescaped" is not relevant if the document ends:
+ *
+ * $at may be here.
+ * ↓
+ * ', true ),
+ 'Script with type attribute' => array( '', true ),
+ 'Script data escaped' => array( '', true ),
+ 'Script data double-escaped exit (comment)' => array( '', true ),
+ 'Script data double-escaped exit (closed)' => array( '', true ),
+ 'Script data double-escaped exit (closed/truncated)' => array( '', true ),
+ 'Script data no double-escape' => array( '', true ),
+
+ 'Script tag with self-close flag (ignored)' => array( '', false ),
+ 'Script data double-escaped' => array( '', false ),
+ );
+ }
+
/**
* Invalid tag names are comments on tag closers.
*