Skip to content

Instantly share code, notes, and snippets.

View Neo42's full-sized avatar
:shipit:

Johnny (Hao) Jiang Neo42

:shipit:
View GitHub Profile
@Neo42
Neo42 / vis.sh
Created March 30, 2021 12:32
Code for changing one's own membership visibility in a github organization
curl \ 20:19:16
-H "Accept: application/vnd.github.v3+json" \
# replace :token below with your personal access token
-H "Authorization: token :token" \
-H "Content-Length: 0" \
-X PUT \
# replace ORGNAME with your organization name and USERNAME your username
https://api.github.com/orgs/ORGNAME/public_members/USERNAME
@Neo42
Neo42 / io.js
Last active May 7, 2021 04:05
IO monad in JS
const IO = effect =>
({
effect,
run() {
return this.effect()
},
map(fn) {
if (!fn ?? typeof fn !== 'function') {
throw new Error('.map requires a function argument.')
}
@Neo42
Neo42 / quickSort.js
Last active March 25, 2022 06:43
Functional quick sort in 8 lines (non in-place)
const quickSort = ([pivot, ...rest]) =>
pivot
? [
...quickSort(rest.filter(x => x < pivot)),
pivot,
...quickSort(rest.filter(y => y >= pivot))
]
: []
@Neo42
Neo42 / quickSort.js
Last active March 25, 2022 06:43
Lomuto's quick sort
const quickSort = (arr, low = 0, high = arr.length - 1) => {
if (low < high) {
const p = partition(arr, low, high)
quickSort(arr, low, p - 1)
quickSort(arr, p + 1, high)
}
}
const partition = (arr, low, high) => {
const pivot = arr[high]
@Neo42
Neo42 / quickSort.js
Created May 4, 2021 15:42
Hoare's quicksort. More performant than Lomuto's.
const quickSort = (arr, low = 0, high = arr.length - 1) => {
if (low < high) {
const p = partition(arr, low, high)
quickSort(arr, low, p)
quickSort(arr, p + 1, high)
}
}
const partition = (arr, low, high) => {
const pivot = arr[Math.floor((high + low) / 2)]
let i = low
@Neo42
Neo42 / react-performance-recipes.md
Last active January 8, 2022 11:01
React performance recipes

React Performance Recipes

Problem: Unnecessary Callback Re-Initiation

When you're using a callback function within useEffect, it's hard to predict how callback function will be modified in the future. Only passing in certain variables from the callback to the deps list won't do. Because once the variables passed in gets removed, your dependencies won't work any more. But you still have to provide some values to the dependency list to sync useEffect with the callback change.

const updateLocalStorage = () => window.localStorage.setItem('count', count)
React.useEffect(() => {
 updateLocalStorage()
{
"semi": false,
"embeddedLanguageFormatting": "off",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"proseWrap": "never",
"trailingComma": "all",
"singleQuote": true
}
@Neo42
Neo42 / useState-lazy-initialization.js
Last active December 6, 2021 15:34
useState lazy initialization by passing a callback function
const [name, setName] = React.useState(
someExpensiveComputation(props); // run on every render
)
const [state, setState] = useState(() => {
const initialState = someExpensiveComputation(props); // only run on the initial render
return initialState;
});
@Neo42
Neo42 / useState.js
Created December 14, 2021 20:11
`useState` implementation with `useReducer`
import React from "react";
const useStateReducer = (prevState, dispatchArg) =>
typeof dispatchArg === "function" ? dispatchArg(prevState) : dispatchArg;
const useStateInitializer = (initialArg) =>
typeof initialArg === "function" ? initialArg() : initialArg;
function useState(initialValue) {
return React.useReducer(useStateReducer, initialValue, useStateInitializer);
@Neo42
Neo42 / compound-components.js
Last active January 7, 2022 16:55
React Patterns: Compound Components
// Compound Components w/ `React.Children.map` & `React.cloneElement`
import * as React from 'react'
import {Switch} from '../switch'
function Toggle({children}) {
const [on, setOn] = React.useState(false)
const toggle = () => setOn(!on)
return React.Children.map(children, child =>
// tell if a child is a built-in DOM component
typeof child.type === 'string'