My first code renders the Feladat significantly slower than my 2nd code, why could that…
- User
My first code renders the Feladat significantly slower than my 2nd code, why could that be?
My first code: ***
import React, { useEffect, useState, useCallback } from 'react'; import { Text, View, VirtualizedList, TouchableOpacity } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { AntDesign as Icon } from '@expo/vector-icons'; import * as SplashScreen from 'expo-splash-screen'; import { useFonts } from 'expo-font';
import MathJax from './MathJax'; import { fetchAllData } from '../Database/Db'; import styles from "./Teszt.style"; import ValaszokGroup from "./ValaszokGroup"; import { BASE64_FONT } from './BASE64_FONT'; import { Feladat } from '../Database/Feladat';
const css =
@font-face { font-family: 'Quicksand'; src: url('data:font/woff2;charset=utf-8;base64,${BASE64_FONT}') format('woff2'); font-weight: normal; font-style: normal; } html{ margin: 0; font-size: 5vw; } body{ font-family: Quicksand; font-weight: normal; font-style: normal; };//SplashScreen.preventAutoHideAsync();
export default function Teszt() { const [fontsLoaded] = useFonts({ 'Quicksand-Regular': require('../../assets/fonts/Quicksand-Regular.ttf'), });
const [feladatok, setFeladatok] = useState<Feladat[]>([]); const [visibleFeladatok, setVisibleFeladatok] = useState<Feladat[]>([]); const [currentPage, setCurrentPage] = useState(0); const [selectedValasz, setSelectedValasz] = useState(-1); const [joMegoldas, setJoMegoldas] = useState(0); const [ellenorizve, setEllenorizve] = useState(false);
useEffect(() => { handleFetchFeladatok(); }, []);
const handleFetchFeladatok = () => { fetchAllData() .then((result) => { setFeladatok(result); }) .catch((err) => { console.error('Error fetching feladatok:', err); }); }
useEffect(() => { if (feladatok.length > 0) { setVisibleFeladatok([feladatok[currentPage]]); } }, [currentPage, feladatok]);
const prevPage = () => { const prevPage = currentPage - 1; setCurrentPage(prevPage); };
const nextPage = () => { const nextPage = currentPage + 1; if (nextPage >= feladatok.length) { return; } setCurrentPage(nextPage); setEllenorizve(false); setSelectedValasz(-1) };
const handleSelection = (index: number) => { setSelectedValasz(index); };
const ellenorzes = () => { setEllenorizve(true); };
const handleSelectionWrapper = (i: number, megoldas: number) => { handleSelection(i); setJoMegoldas(megoldas - 1); };
const renderItem = ({ item }: { item: Feladat }) => {
return ( <View key={item.ID}> <View> <View style={styles.feladatSzamTextContainer}> <Text style={styles.feladatSzamText}>{item.Feladatszam}.</Text> </View> <View style={styles.kerdesContainer}> <MathJax showsVerticalScrollIndicator={false} overScrollMode="never" style={styles.kerdesMathJax} html={`<head><style>${css}</style></head><body> <p style='user-select: none; hyphens: auto; overflow: hidden; text-indent: 50px;'>${item.Kerdes}</p> </body>`} /> </View> </View> <ValaszokGroup valasz={[item.Valasz1, item.Valasz2, item.Valasz3, item.Valasz4]} jo={joMegoldas} ellenorizve={ellenorizve} selectedIndex={selectedValasz} onSelection={(index: number) => handleSelectionWrapper(index, item.Megoldas)} /> </View> ); - Assistant
The main difference between your first and second code are:
-
Font loading and usage: In the first code, you are using the BASE64_FONT for loading the custom font in the MathJax CSS. However, in the second code, you are not using any custom font for MathJax.
-
Content rendering: In the first code, you wrap the MathJax content with a and tags that contain the custom font information. In the second code, you are rendering the MathJax content directly without any additional style information.
-
SVG handling: In the second code, you are checking for SVGs and rendering them using the SvgXml component from the react-native-svg package. This part is not present in the first code.
-
- User
- Assistant
The main reason for the slower rendering of the Feladat in the first code compared to the second code could be due to the loading of the custom font and the way it is used in MathJax. The custom font is loaded using a base64-encoded string in the MathJax CSS in the first code.
In the second code, you don't use a custom font for MathJax. Instead, you rely on the system font, which is faster to load and render.
Additionally, the first code uses more complex HTML content with the custom font through
<head>and<style>tags for the MathJax component. In contrast, the second code relies on simpler HTML content without any additional styling information, which contributes to a faster rendering time on the second code.To improve the performance of the first code, you can try the following:
-
Remove the custom font from MathJax CSS and use a system font instead, just like in the second code.
-
Simplify the content rendered with MathJax by removing the
<head>and<style>tags (if not needed). This will help reduce the complexity of the rendered content and improve performance.
-
- User
I used the same code as my friend (only I wrote it in typescript), deleted everything else and my rendering is like 4-5 times slower than his, why could that be?
- Assistant
If you're using the same code as your friend but experiencing significantly slower rendering, the issue might not be related to the code itself but rather to some external factors. Some possible reasons for the slower rendering include:
-
Device performance: The device you are using to run the application might have lower performance specs compared to your friend's device. Lower-performance devices may take more time to process and render the same code compared to higher-performance devices.
-