Skip to content

Instantly share code, notes, and snippets.

View ezekielchentnik's full-sized avatar

Ezekiel Chentnik ezekielchentnik

View GitHub Profile
https://cdn.shopify.com/s/files/1/1338/7937/files/Bootstrap_Farmer_Ultimate_Microgreen_Cheatsheet_Printable.pdf?v=1604180053
export class RingBuffer {
constructor(capacity, data = []) {
this._capacity = capacity;
this._buffer = new Array(this._capacity);
this._writeIndex = 0;
this._readIndex = 0;
this._msgsReceived = 0;
this._lastMeasurement = performance.now();
this._msgsSinceLast = 0;
const fps = {
sampleSize : 60,
value : 0,
_sample_ : [],
_index_ : 0,
_lastTick_: false,
tick : function(){
// if is first tick, just set tick timestamp and return
if( !this._lastTick_ ){
this._lastTick_ = performance.now();
// we augment the window.history instance so that it will dispatch an event when using push/replace
// this allows us to create event stream and listen to all history state changes
export const patchHistory = history => {
const wrap = m => (state, title, url) => {
m.call(history, state, title, url);
let e = new Event(m.name.toLowerCase());
e.state = state;
dispatchEvent(e);
};
@ezekielchentnik
ezekielchentnik / virtual-list.js
Created August 26, 2019 19:00
Virtualized (Windowed) List w/ React hooks
/**
* VirtualList: a universal windowed list for large datasets
*
* @param {Array} data - array of rows to render
* @param {string} rowHeight - height of row container in pixels
* @param {function} renderRow - function to render a single homogenous row
* @param {number} overscanCount - number of extra rows to pre-render ahead/behind the window
* @param {Object} rest - additional props
*
* @example
@ezekielchentnik
ezekielchentnik / scheduler.js
Created July 27, 2019 01:35
Request Idle Callback Scheduler
// Wait at most one frame before processing events.
const BUDGET = 16.7;
let isRequestIdleCallbackScheduled = false;
const scheduleWork = () => {
// Only schedule the rIC if one has not already been set.
if (isRequestIdleCallbackScheduled) {
return;
}
@ezekielchentnik
ezekielchentnik / index.js
Created July 25, 2019 15:31
Leak detection
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Memory Leak</title>
</head>
<body>
<h1>Memory Leak</h1>
function relativize(url, to) {
let [,base,origin] = to.match(/^(([a-z]+:\/\/[^\/]+)(\/.*?)?)(\/[^\/]*)?$/i)
url = String(url).replace(/^\/([^\/])/g, `${origin}/$1`);
if (!url.match(/^([a-z]+\:)\/\//i)) url = `${base}/${url}`;
return url;
}
@ezekielchentnik
ezekielchentnik / pool.js
Created July 22, 2019 05:09
Three.js Object pool
// function removeObject (object, pool) {
// var tempObj;
// for (var i = 0; i < pool.length; i++) {
// if ( pool[i] === object ) {
// pool.activeLength --;
// canvas.scene.remove( pool[i] );
// tempObj = pool[i];
// pool[pool[i]] = pool[pool.length - 1];
// return;
// }
@ezekielchentnik
ezekielchentnik / Loop.js
Last active July 22, 2019 04:27
Three.js Game Loop
import { EventDispatcher } from "three";
export default class Loop extends EventDispatcher {
constructor(fps = 60, speed = 1) {
super();
this._timeStep = (1000 / fps) * speed;
this._prevTime = null;
this._lagTime = 0;