perf improvement - lower compute

This commit is contained in:
Michael Freno
2026-01-04 21:15:05 -05:00
parent 8c8ae6be42
commit c6305d2f07

View File

@@ -1,4 +1,4 @@
import { JSX, onMount, createSignal, children } from "solid-js"; import { JSX, onMount, onCleanup, createSignal, children } from "solid-js";
export function Typewriter(props: { export function Typewriter(props: {
children: JSX.Element; children: JSX.Element;
@@ -21,31 +21,28 @@ export function Typewriter(props: {
containerRef.style.position = "relative"; containerRef.style.position = "relative";
const textNodes: { node: Text; text: string; startIndex: number }[] = [];
let totalChars = 0; let totalChars = 0;
const charElements: HTMLElement[] = [];
const walkDOM = (node: Node) => { const walkDOM = (node: Node) => {
if (node.nodeType === Node.TEXT_NODE) { if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent || ""; const text = node.textContent || "";
if (text.trim().length > 0) { if (text.trim().length > 0) {
textNodes.push({
node: node as Text,
text: text,
startIndex: totalChars
});
totalChars += text.length; totalChars += text.length;
const fragment = document.createDocumentFragment();
const span = document.createElement("span"); const span = document.createElement("span");
text.split("").forEach((char, i) => {
text.split("").forEach((char) => {
const charSpan = document.createElement("span"); const charSpan = document.createElement("span");
charSpan.textContent = char; charSpan.textContent = char;
charSpan.setAttribute( charSpan.style.opacity = "0";
"data-char-index", charElements.push(charSpan);
String(totalChars - text.length + i)
);
span.appendChild(charSpan); span.appendChild(charSpan);
}); });
node.parentNode?.replaceChild(span, node);
fragment.appendChild(span);
node.parentNode?.replaceChild(fragment, node);
} }
} else if (node.nodeType === Node.ELEMENT_NODE) { } else if (node.nodeType === Node.ELEMENT_NODE) {
Array.from(node.childNodes).forEach(walkDOM); Array.from(node.childNodes).forEach(walkDOM);
@@ -63,25 +60,33 @@ export function Typewriter(props: {
cursorRef?.removeEventListener("animationend", handleAnimationEnd); cursorRef?.removeEventListener("animationend", handleAnimationEnd);
}; };
let cleanupAnimation: (() => void) | undefined;
const startReveal = () => { const startReveal = () => {
setIsTyping(true); setIsTyping(true);
let currentIndex = 0; let currentIndex = 0;
const speed = props.speed || 30; const speed = props.speed || 30;
const msPerChar = 1000 / speed;
let lastTime = performance.now();
let animationFrameId: number;
const revealNextChar = () => { const revealNextChar = (currentTime: number) => {
const elapsed = currentTime - lastTime;
if (elapsed >= msPerChar) {
if (currentIndex < totalChars) { if (currentIndex < totalChars) {
const charSpan = containerRef?.querySelector( const charSpan = charElements[currentIndex];
`[data-char-index="${currentIndex}"]`
) as HTMLElement;
if (charSpan) { if (charSpan) {
// Batch style reads first
const rect = charSpan.getBoundingClientRect();
const containerRect = containerRef?.getBoundingClientRect();
// Then batch style writes
charSpan.style.opacity = "1"; charSpan.style.opacity = "1";
if (cursorRef && containerRef) { if (cursorRef && containerRect) {
const rect = charSpan.getBoundingClientRect();
const containerRect = containerRef.getBoundingClientRect();
cursorRef.style.left = `${rect.right - containerRect.left}px`; cursorRef.style.left = `${rect.right - containerRect.left}px`;
cursorRef.style.top = `${rect.top - containerRect.top}px`; cursorRef.style.top = `${rect.top - containerRect.top}px`;
cursorRef.style.height = `${charSpan.offsetHeight}px`; cursorRef.style.height = `${charSpan.offsetHeight}px`;
@@ -89,7 +94,7 @@ export function Typewriter(props: {
} }
currentIndex++; currentIndex++;
setTimeout(revealNextChar, 1000 / speed); lastTime = currentTime;
} else { } else {
setIsTyping(false); setIsTyping(false);
@@ -102,20 +107,56 @@ export function Typewriter(props: {
cursorRef.style.animation = `blink 1s ${iterations}`; cursorRef.style.animation = `blink 1s ${iterations}`;
} }
} }
return;
} }
}
animationFrameId = requestAnimationFrame(revealNextChar);
}; };
setTimeout(revealNextChar, 100); animationFrameId = requestAnimationFrame(revealNextChar);
return () => {
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
};
}; };
if (delay > 0) { if (delay > 0) {
setTimeout(() => { setTimeout(() => {
setIsDelaying(false); setIsDelaying(false);
startReveal(); cleanupAnimation = startReveal();
}, delay); }, delay);
} else { } else {
startReveal(); cleanupAnimation = startReveal();
} }
// Use IntersectionObserver to pause animation when not in viewport
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// If component leaves viewport while animating, we could pause
// For now, we just ensure it starts when visible
if (!entry.isIntersecting && cleanupAnimation) {
// Component is off-screen - could add pause logic here if needed
}
});
},
{
rootMargin: "50px", // Start slightly before entering viewport
threshold: 0.1
}
);
observer.observe(containerRef);
onCleanup(() => {
observer.disconnect();
if (cleanupAnimation) {
cleanupAnimation();
}
});
}); });
const getCursorClass = () => { const getCursorClass = () => {
@@ -129,6 +170,7 @@ export function Typewriter(props: {
<div <div
ref={containerRef} ref={containerRef}
class={props.class} class={props.class}
style={{ opacity: animated() ? "1" : "0" }}
data-typewriter={!animated() ? "static" : "animated"} data-typewriter={!animated() ? "static" : "animated"}
> >
{resolved()} {resolved()}