Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View laphilosophia's full-sized avatar
🖖
live long and prosper

Erdem Arslan laphilosophia

🖖
live long and prosper
View GitHub Profile
@laphilosophia
laphilosophia / areEquivalent.js
Created December 10, 2021 10:55 — forked from DLiblik/areEquivalent.js
Fast JS function for testing if Javascript values/objects are equivalent or equal - guards against circular references, prioritizes speed.
/**
Compares two items (values or references) for nested equivalency, meaning that
at root and at each key or index they are equivalent as follows:
- If a value type, values are either hard equal (===) or are both NaN
(different than JS where NaN !== NaN)
- If functions, they are the same function instance or have the same value
when converted to string via `toString()`
- If Date objects, both have the same getTime() or are both NaN (invalid)
- If arrays, both are same length, and all contained values areEquivalent
@laphilosophia
laphilosophia / promise_map.js
Created May 4, 2021 12:29 — forked from tokland/promise_map.js
Execute promises sequentially (one at at a time) and return array with the results
function promiseMap(inputValues, mapper) {
const reducer = (acc$, inputValue) =>
acc$.then(acc => mapper(inputValue).then(result => acc.push(result) && acc));
return inputValues.reduce(reducer, Promise.resolve([]));
}
/* Example */
const axios = require('axios');
@laphilosophia
laphilosophia / fpsMeter.js
Created December 21, 2020 18:21 — forked from capfsb/fpsMeter.js
JavaScript FPS meter - Calculating frames per second
fpsMeter() {
let prevTime = Date.now(),
frames = 0;
requestAnimationFrame(function loop() {
const time = Date.now();
frames++;
if (time > prevTime + 1000) {
let fps = Math.round( ( frames * 1000 ) / ( time - prevTime ) );
prevTime = time;
@laphilosophia
laphilosophia / http2.js
Created November 26, 2019 21:06 — forked from davidgilbertson/http2.js
HTTP2 server with compression and caching
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const brotli = require('brotli'); // npm package
const PORT = 3032;
const BROTLI_QUALITY = 11; // slow, but we're caching so who cares
const STATIC_DIRECTORY = path.resolve(__dirname, '../dist/');
const cache = {};
@laphilosophia
laphilosophia / axios.js
Created November 6, 2019 19:57 — forked from wooooodward/axios.js
Axios plugin example with request interceptor that adds JWT token to the auth header and 401 response interceptor to refresh token
import Vue from 'vue'
import axios from 'axios'
import store from '../store'
import { TokenService } from '../services/storage.service'
// Full config: https://github.com/axios/axios#request-config
let config = {
baseURL:
process.env.baseURL ||
@laphilosophia
laphilosophia / js-turkish-to-english.js
Created February 25, 2019 11:36 — forked from enginkartal/js-turkish-to-english.js
Javascript Turkish character to english characters change
String.prototype.turkishtoEnglish = function () {
return this.replace('Ğ','g')
.replace('Ü','u')
.replace('Ş','s')
.replace('I','i')
.replace('İ','i')
.replace('Ö','o')
.replace('Ç','c')
.replace('ğ','g')
.replace('ü','u')
@laphilosophia
laphilosophia / what-forces-layout.md
Created February 15, 2019 07:20 — forked from paulirish/what-forces-layout.md
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Element

Box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
  • elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight
  • elem.getClientRects(), elem.getBoundingClientRect()
@laphilosophia
laphilosophia / README.md
Created November 6, 2018 08:30 — forked from datchley/README.md
Micro templating library for javascript

Micro-Template: README.md

Valid template expressions:

  • expressions can reference any property on the passed in context, including nested properties and indexed access to array properties.
     {{ name }}
 {{ obj.name }}
@laphilosophia
laphilosophia / cookies.js
Created October 4, 2018 17:09 — forked from arozwalak/cookies.js
Javascript: Cookies set/get/delete
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
}
else {
@laphilosophia
laphilosophia / IndexedDB101.js
Created July 24, 2018 14:20 — forked from JamesMessinger/IndexedDB101.js
Very Simple IndexedDB Example
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
// Open (or create) the database
var open = indexedDB.open("MyDatabase", 1);
// Create the schema
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});