Skip to content

Instantly share code, notes, and snippets.

View hungify's full-sized avatar
🎯
Stay focused, achieve more

hungify hungify

🎯
Stay focused, achieve more
View GitHub Profile
@hungify
hungify / flat-tree.ts
Created January 19, 2025 04:01
Optimizing Tree-like Data Structures
import { performance } from "perf_hooks";
type TreeNode = {
id: number;
name: string;
parentId: number | null;
children: number[];
};
type NormalizedTree = {
@hungify
hungify / react.md
Last active May 15, 2024 15:49
Summary about React Ecosystem

Summary

  • React always recursively renders components by default, so when a parent renders, its children will render
  • Rendering by itself is fine - it's how React knows what DOM changes are needed
  • But, rendering takes time, and "wasted renders" where the UI output didn't change can add up
  • It's okay to pass down new references like callback functions and objects most of the time APIs like React.memo() can skip unnecessary renders if props haven't changed
  • But if you always pass new references down as props, React.memo() can never skip a render, so you may need to memoize those values
  • Context makes values accessible to any deeply nested component that is interested
  • Context providers compare their value by reference to know if it's changed
  • A new context values does force all nested consumers to re-render but, many times the child would have re-rendered anyway due to the normal parent->child render cascade process
@hungify
hungify / refresh-token-machine.tsx
Last active May 12, 2024 08:21
Refresh Token Machine (X Time before expiration)
"use client";
import dayjs from "dayjs";
import { jwtDecode } from "jwt-decode";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";
import { useAuthContext } from "../auth/context";
export default function RefreshTokenMachine() {
const [diff, setDiff] = useState<number>(1000);
@hungify
hungify / alpine_cron_job.md
Last active December 3, 2023 09:40
Running cron job inside a Docker container with Alpine distribution.

Running cron job inside a Docker container with Alpine distribution

With Docker

alpine.dockerfile

FROM alpine

RUN which crond && rm -rf /etc/periodic