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 / deploy-to-firebase.sh
Created December 10, 2016 10:43
deploy-to-firebase Bash script
#! /bin/bash
clear
echo -e "#1 - Removing build/ directory for a fresh build of the project..."
if [ -d "build/" ]; then
rm -rf build/
fi
echo -e "\n#2 = Building project..."
@motss
motss / errors-indexeddb-sw.md
Created December 12, 2016 04:44
app-indexeddb-mirror not copied in build & not cached by sw
@motss
motss / feature-detect-css-properties.js
Created December 15, 2016 05:47
Feature detection for Native CSS Properties aka CSS Custom Variables
// Not too sure if it will trigger layout and paint for other CSS properties.
// I just use opacity which only triggers composite by default!
function hasNativeCSSProperties() {
const opacity = 1;
const el = document.body;
let hasNativeCSSProperties = false;
// Setup CSS properties.
el.style.setProperty('--test-opacity', opacity);
el.style.setProperty('opacity', 'var(--test-opacity)');
@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 / vscode-snippets-simple-guid.md
Last active January 15, 2017 16:23
vscode-snippets-simple-guide

Simple pre-requisites.

  1. npm install -g vsce
  2. Create a free account at https://www.visualstudio.com/en-us/get-started/setup/sign-up-for-visual-studio-online
  3. Create your publisher name - vsce create-publisher (publisher name)
  4. Create Extension manifest (package.json). More details at https://code.visualstudio.com/docs/extensionAPI/extension-manifest
  5. vsce publish // OR vsce publish major|minor|patch
  6. DONE. Your snippet is now up on VS Code's Extensions marketplace.
@motss
motss / convert-native-date-object-with-timezone-offset.js
Created January 12, 2017 08:48
Convert native Date object with timezone offset
d = new Date();
dl = new Date(d.getTime() - d.getTimezoneOffset() * 60 * 1E3);
console.log(d);
console.log(dl.toISOString());
console.log(d.getTimezoneOffset / -60);
@motss
motss / appendChild-using-rAF.js
Created January 18, 2017 03:16
How to appendChild on each 16ms frame using rAF
// This is render blocking the main thread.
const tasks = 1E6;
for (let i = 0; i < tasks; i++) {
window.requestAnimationFrame(() => {
const el = document.createElement('div');
el.style.height = '64px';
el.textContent = i;
document.body.appendChild(el);
@motss
motss / js-experiment-errors.js
Last active February 23, 2017 06:30
Experimenting on errors in Javascript with Promise and try...catch
try {
throw new Error('failed before promise'); // try...catch: Error: failed before promise.
Promise.resolve()
.then(() => {
throw new Error('fatal error'); // Promise failed: Error: fatal error.
})
.catch(err => {
console.error('Promise failed: ', err);
return Promise.reject(err); // Uncaught Exception: Error: fatal error.
@motss
motss / long-polling-with-interval-and-max-duration.js
Last active March 7, 2017 08:38
Long Polling with interval and max duration with ES6+
/**
* Long polling happens in each successive `interval` and the long polling only allows to continue for `maxDuration`.
* `maxDuration` determines the time at which enough useful information has been gathered over the intervals,
* after all, things that too slow possess lesser significance by assumptions else they need to be improved
* if they are really important as no important/ high priority should load this slow.
* See [RAIL model](https://developers.google.com/web/fundamentals/performance/rail) for more info.
*
* Required parameters:
* 1. interval - how long the subsequent polling starts.
* 2. max duration - time reached to send out whatever has been gathered at that point.
@motss
motss / naive-masonry.js
Created March 11, 2017 14:35
Naive mansory layout with CSS transform.
{
const containerWidth = window.innerWidth;
const maxImgWidth = containerWidth / 4;
const allImg = [...document.querySelectorAll('#grid > .item')];
console.log(maxImgWidth);
allImg.map((img, idx) => {
const cachedRect = img.getBoundingClientRect();
const aspectRatio = cachedRect.width / cachedRect.height;