Skip to content

Instantly share code, notes, and snippets.

@night-fury-rider
night-fury-rider / performance-throttling.js
Created May 16, 2025 03:30
Performance - Throttling
// Make sure we have a input field with id "inputData" in our DOM.
const inputData = document.getElementById("inputData");
const getDataFromServer = (name) => {
console.log(`${name} :: getting Data from Server...........`);
};
const throttle = (callback, delay) => {
delay = delay || 2000;
@night-fury-rider
night-fury-rider / performance-debounce.js
Created May 16, 2025 03:17
Performance - Debounce
// Make sure we have a input field with id "inputData" in our DOM.
const inputData = document.getElementById("inputData");
const getDataFromServer = (name) => {
console.log(`${name} :: getting Data from Server...........`);
};
const debounce = (callback, delay) => {
delay = delay || 2000;
@night-fury-rider
night-fury-rider / design-pattern-memoization.js
Created May 16, 2025 01:43
Design Pattern - Memoization
const memoize = (func) => {
const cache = {};
return (...args) => {
const key = args.toString();
if (cache[key]) {
console.log(`Getting from the cache for ${key}`);
return cache[key];
} else {
@night-fury-rider
night-fury-rider / design-pattern-async-iterator.js
Created May 16, 2025 01:35
Design Pattern - Async Iterator
class MyAsyncIterator {
constructor() {
this.queue = []; // To hold actual values
this.waitlist = []; // To hold the callbacks
}
/**
* If the waitlist contains an item, remove it and execute it.
* Otherwise, add the value to the queue.
*/
@night-fury-rider
night-fury-rider / design-pattern-observable.js
Created May 16, 2025 00:49
Design Pattern - Observable
class Stock {
constructor(symbol) {
this.symbol = symbol;
this.price = 0;
this.observers = [];
}
// Subscribe to stock price changes
subscribe(observer) {
this.observers.push(observer);
@night-fury-rider
night-fury-rider / styles.css
Last active May 13, 2025 03:14
CSS - Common Styles
body {
background-color: skyblue;
margin: 20px;
height: 90%;
}
.flex_center {
display: flex;
align-items: center;
justify-content: center;
@night-fury-rider
night-fury-rider / vscode-mac-snippets-html.json
Last active May 13, 2025 02:53
Visual Studio Code - Mac - HTML Boilerplate Snippets
// Make these changes in the ~/Library/Application Support/Code/User/snippets/html.json
// Place your snippets for html here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
@night-fury-rider
night-fury-rider / design-pattern-pubsub.js
Last active May 13, 2025 02:31
Design Pattern - Pub/Sub
class PubSub {
constructor() {
this.events = {};
}
subscribe(eventName, callback) {
if (!this.events[eventName]) {
this.events[eventName] = [];
}
this.events[eventName].push(callback);
@night-fury-rider
night-fury-rider / jest-render-test-component-using-method.test.ts
Last active February 12, 2025 04:51
jest - Render Test Component using Method
import { Provider } from "react-redux";
import thunk, { ThunkMiddleware } from "redux-thunk";
import { render, RenderAPI } from "react-test-renderer";
import configureMockStore from "redux-mock-store";
import TestComponent from "./TestComponent";
import { describe } from "node:test";
const middlewares: Array<ThunkMiddleware> = [thunk];
const createMockStore = configureMockStore(middlewares);
@night-fury-rider
night-fury-rider / jest-mocking-partial-mock-library-methods-1.test.tsx
Created February 10, 2025 08:49
Jest - Partially Mocking Library Methods 1
import Notifee from "@notifee/react-native";
jest.spyOn(Notifee, "requestpermission");
Notifee.requestPermission.mockImplementation(() => {
throw new Error();
});