'Does type guard function affect the performance of the app?

const typeGuard = (param: any): param is SomeType => {
   return (
     !!param &&
     typeof param === "object" &&
     param.someProperty1 !== null &&
     param.someProperty2 === null
   )
}

If there is a type guard function like the code above and the function is executed more than 1000 times,

Does it affect the performace of the app??

(EDIT)

My app is React app and type guard function will be executed about 1400 times when data is fetched

Example code is

  useEffect(() => {
    const dataFetch = () => {
      fetch().then((response) => {
        // this part will be executed about 1400 times
        const newState = response.map((data: unknown) => {
          if (typeGuard(data)) {
            return { processedData };
          }
        });
        setData(newState);
      });
    };

    dataFetch();

    if (delay !== null) {
      const interval = setInterval(dataFetch, delay);

      return () => {
        clearInterval(interval);
      };
    }
  }, [delay]);


Solution 1:[1]

Yes, since the body of your type guard is actually being executed at runtime - it does affect runtime performance.

How much does it affect it? Well, you could do your measurements, I could just propose to play with console.time() (link)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kostiantyn Ko