Skip to content

Instantly share code, notes, and snippets.

View adardesign's full-sized avatar
🎯
Focusing

Eliazer Braun adardesign

🎯
Focusing
View GitHub Profile
@mmocny
mmocny / inp-devtools-watch.js
Created April 14, 2023 14:12
Add these snippets to DevTools (console) Watch expressions
// max-INP:
(()=>{let o=globalThis;return void 0===o.winp&&(o.winp=0,new PerformanceObserver(n=>{for(let e of n.getEntries()){if(!e.interactionId)continue;o.winp=Math.max(e.duration,o.winp);let r=o=>o<=200?"color: green":o<=500?"color: yellow":"color: red";console.log(`%c[Interaction: ${e.name.padEnd(12)}] %cDuration: %c${e.duration}`,"color: grey; font-family: Consolas,monospace","",r(e.duration))}}).observe({type:"event",durationThreshold:0,buffered:!0})),o.winp})();
// interactionCount
performance.interactionCount;
@malchata
malchata / get-selector.js
Last active April 4, 2024 09:10
A module that returns a selector string for a DOM node.
// Cribbed this from:
// https://web.dev/debug-web-vitals-in-the-field/#usage-with-the-web-vitals-javascript-library
export function getSelector(node, maxLen = 100) {
let sel = '';
try {
while (node && node.nodeType !== 9) {
const part = node.id ? '#' + node.id : node.nodeName.toLowerCase() + (
(node.className && node.className.length) ?
'.' + Array.from(node.classList.values()).join('.') : '');
@pmeenan
pmeenan / worker.js
Created November 8, 2021 20:21
Adding Priority Hints with Cloudflare Workers
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
const url = new URL(request.url);
const host = request.headers.get('x-host');
if(!host) {
return new Response('x-host header missing', {status: 403});
}
@tkadlec
tkadlec / perf-diagnostics.css
Last active August 17, 2024 19:33
CSS used to highlight potential performance issues
:root {
--violation-color: red; /* used for clear issues */
--warning-color: orange; /* used for potential issues we should look into */
}
/* IMAGES */
/*
* Lazy-Loaded Images Check
* ====
@trmcnvn
trmcnvn / object.js
Last active December 23, 2021 21:59
Reuse IntersectionObserver's
export function sortObject(object) {
Object.keys(object).reduce((prev, curr) => {
if (object[curr] !== null && typeof object[curr] === 'object') {
prev[curr] = sortObject(object[curr]);
return prev;
}
prev[curr] = object[curr];
return prev;
}, {});
}
@astoilkov
astoilkov / readme.md
Last active November 16, 2024 12:52
Async Operations with useReducer Hook

Async Operations with useReducer Hook

9 March, 2019

We were discussing with @erusev what we can do with async operation when using useReducer() in our application. Our app is simple and we don't want to use a state management library. All our requirements are satisfied with using one root useReducer(). The problem we are facing and don't know how to solve is async operations.

In a discussion with Dan Abramov he recommends Solution 3 but points out that things are fresh with hooks and there could be better ways of handling the problem.

Problem

@DavidKuennen
DavidKuennen / minimal-analytics-snippet.js
Last active October 24, 2024 18:10
Minimal Analytics Snippet
(function (context, trackingId, options) {
const history = context.history;
const doc = document;
const nav = navigator || {};
const storage = localStorage;
const encode = encodeURIComponent;
const pushState = history.pushState;
const typeException = 'exception';
const generateId = () => Math.random().toString(36);
const getId = () => {
@radum
radum / A Web performance resources.md
Last active September 4, 2024 21:49
Web performance resources
@jtrein
jtrein / ErrorBoundary.js
Created October 27, 2017 10:21
React Error Boundary Component and HOC
import React, { Component } from 'react';
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch = (error, errorInfo) => catchFunc(error, errorInfo, this)
@paulirish
paulirish / intro-overhead-of-performance.mark.md
Last active April 2, 2024 16:50
Evaluating overhead of performance.mark()

A few conversations have circled around user-side structural profiling. For context, see React PR #7549: Show React events in the timeline when ReactPerf is active

One particular concern is the measurement overhead. This gist has a benchmarking script (measure.js) for evaluating overhead and initial results.

Results: performance.mark()

Runs about 0.65µs per mark() call. Naturally, that's ~= an overhead of 1ms for 1500 mark()s. image