Skip to content

Instantly share code, notes, and snippets.

View motss's full-sized avatar
🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!

The web walker motss

🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!
View GitHub Profile
@motss
motss / mru-using-doubly-linked-list.js
Last active April 18, 2024 17:48
MRU in JavaScript using Doubly linked list
class Node {
constructor(value) {
this.prev = null;
this.next = null;
this.value = value;
}
}
class MRU {
constructor(size) {
@motss
motss / tips_increase_memory_limit_nodejs.md
Last active April 9, 2024 06:24
[TIPS] Increase memory limit in Node.js

By default the memory limit in Node.js is 512MB.

This will cause FATAL ERROR- JS Allocation failed – process out of memory when processing large data files.

It can be avoided by increasing the memory limit.

See Command-line options for more details.

node --max-old-space-size=1024 server.js # increase to 1gb
@motss
motss / config.md
Created November 19, 2017 02:49
.editorconfig + .gitattributes

Config

.editorconfig

# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# http://editorconfig.org

root = true
@motss
motss / expression-parser-multi-line.js
Last active October 19, 2023 11:50
Expression parser
// a = 1; b = a - 1; b = b - 100; b * a - 100
// split('; ');
// a=1
// b=a-1
// b=b-100
// b*a-100
// a=1
// a 1 =
@motss
motss / get-bundlejs-badge-url.js
Last active April 27, 2023 17:51
Generate custom badge URL using shields.io and deno.bundlejs.com
function tryParseUrl(url) {
try {
const u = new URL(url);
return u;
} catch (error) {
console.error(error);
return '';
}
}
@motss
motss / LitElement_mobx.ts
Last active January 14, 2023 21:18
Best of both worlds: lit-element + mobx
import { html, LitElement } from 'https://unpkg.com/lit-element@latest/lit-element.js?module';
import { observable, action, autorun, reaction } from 'https://unpkg.com/mobx@latest/lib/mobx.es6.js?module';
const store = observable({
title: 'Hello, World!',
count: 0,
incrementCount: action(() => {
store.count += 1;
}),
@motss
motss / uuid-v4-browser.js
Created July 3, 2019 03:33
UUID v4 for browsers
/** See https://bit.ly/2gvCcqe to learn more about UUID v4 for browsers */
function uuidv4() {
if (window.crypto && window.crypto.getRandomValues) {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
@motss
motss / crack-the-event-loop-2.js
Last active October 4, 2021 13:17
Crack the event loop
function main() {
console.log('a');
const a = new Promise((resolve) => {
resolve('b');
console.log('c');
}).then(console.log);
setTimeout(async () => {
b();
@motss
motss / switch-to-zsh-in-bash.md
Last active August 6, 2021 13:49
Switch to ZSH Shell in Bash
@motss
motss / find-sum-of-2-smallest-numbers.js
Last active October 22, 2020 05:31
Find the sum of 2 smallest numbers
function findSmallestSum(arr) {
const len = arr.length;
if (!len) return 0;
if (len === 1) return arr[0];
if (len === 2) return arr[0] + arr[1];
// f - first, s - second
let [f, s] = arr;
let sum = f + s;