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 / April 18, 2022.md
Last active March 6, 2023 09:44
gistpad-scratch

Rust Jargon Explained

Scope

  • Definition: Region of the program where a variable is valid.
  • A variable becomes valid when it comes into scope.
  • A variable remains valid until it goes out of scope.
  • Variable bindings are constrained to live within a block of code where it was created.
  • Blocks of code are enclosed by curly braces: {}
@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 / control-props.js
Created December 17, 2021 11:47
React Patterns: Control Props
// Control Props
import * as React from 'react'
import warning from 'warning'
import {Switch} from '../switch'
const mergeAll =
(...fns) =>
(...args) =>
fns.forEach(fn => fn?.(...args))
@Neo42
Neo42 / readme.md
Last active March 8, 2022 10:18
how webpack works inside

How code splitting works in webpack?

  1. get the chunkIds and moreModules
  2. for each chunkId, if the chunk is previously cached, use that cached version
  3. load all the modules in moreModules
  4. run __webpack_require__.e when needed
    1. find the head, create the script tag
    2. send a JSONP request, get the code
    3. append the script to the head
@Neo42
Neo42 / config.bash
Created February 16, 2022 05:35
how to fix git connection issue
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --local --unset http.proxy
git config --local --unset https.proxy
git config --global http.proxy http://127.0.0.1:8001
git config --global https.proxy https://127.0.0.1:8001
@Neo42
Neo42 / readme.md
Last active January 20, 2022 08:07
When to use code splitting (dynamic import) in webpack?
  1. Heavy library but not needed initially (threejs)
  2. Temporal components (that come and go or are conditionally loaded): tooltips, model, notification, dialog, page scrolling
  3. Routes
@Neo42
Neo42 / __webpack_require__.js
Created January 19, 2022 16:15
__webpack_require__
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId]
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports
/******/
}
/******/ // Create a new module (and put it into the cache)
/******/ var module = (__webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
@Neo42
Neo42 / promise.js
Last active January 18, 2022 10:43
Simple promise implementation.
const states = {
PENDING: 'PENDING',
FULFILLED: 'FULFILLED',
REJECTED: 'REJECTED',
}
const isThenable = (thing) => thing && typeof thing.then === 'function'
class MyPromise {
_state = states.PENDING
@Neo42
Neo42 / command.js
Last active January 17, 2022 14:56
Design patterns in JavaScript
class OrderManager {
orders = new Set()
execute(command, ...args) {
return command.run(this.orders, ...args)
}
}
class Command {
constructor(steps) {