From 3b5a3d9d0269ce225af824ea1c7f39e298522035 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 9 Jul 2026 01:49:28 +0000 Subject: [PATCH] perf: optimize speech synthesis voice lookup by caching preferred voice Co-authored-by: pavan721 <88892612+pavan721@users.noreply.github.com> --- src/components/layout/ChatInterface.tsx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/components/layout/ChatInterface.tsx b/src/components/layout/ChatInterface.tsx index 9980228..3993702 100644 --- a/src/components/layout/ChatInterface.tsx +++ b/src/components/layout/ChatInterface.tsx @@ -11,6 +11,7 @@ function useSpeechSynthesis() { const [isMuted, setIsMuted] = useState(false) const synth = typeof window !== 'undefined' ? window.speechSynthesis : null const { setIsSpeaking } = useAppStore() + const cachedVoiceRef = useRef(null) const speak = (text: string) => { if (isMuted || !synth) return @@ -20,16 +21,22 @@ function useSpeechSynthesis() { const utterance = new SpeechSynthesisUtterance(text) - // Try to find an English robotic/male voice, fallback to any available english voice - const voices = synth.getVoices() - let preferredVoice = voices.find(v => v.name.includes('Google UK English Male') || v.name.includes('Daniel') || v.name.includes('Male') || v.name.toLowerCase().includes('jarvis')) + if (!cachedVoiceRef.current) { + // Try to find an English robotic/male voice, fallback to any available english voice + const voices = synth.getVoices() + let preferredVoice = voices.find(v => v.name.includes('Google UK English Male') || v.name.includes('Daniel') || v.name.includes('Male') || v.name.toLowerCase().includes('jarvis')) - if (!preferredVoice) { - preferredVoice = voices.find(v => v.lang.startsWith('en')) + if (!preferredVoice) { + preferredVoice = voices.find(v => v.lang.startsWith('en')) + } + + if (preferredVoice) { + cachedVoiceRef.current = preferredVoice + } } - if (preferredVoice) { - utterance.voice = preferredVoice + if (cachedVoiceRef.current) { + utterance.voice = cachedVoiceRef.current } utterance.pitch = 0.8 // Slightly deeper