Skip to content

Instantly share code, notes, and snippets.

View jpvincent's full-sized avatar

Jean-Pierre Vincent jpvincent

View GitHub Profile
@jpvincent
jpvincent / optimizedFor.js
Last active November 25, 2021 17:12
never blocking loop mechanism, using setTimeout 0 trick and time control to break the loop and let the browser breath
/**
* NEVER BLOCKING LOOP : implementation of the infamous setTimeout 0 hack, with time checking in order to guarantee fluidity without sacrificing execution speed.
*
* USAGE :
* var array = ["a way too big array that is heavy to process"]
* optimizeFor({
* nbIterations: array.length,
* each:function( index ) {
* doSomethingUsefulWith( array[ index ] );
* },
const timings = performance.getEntriesByType('navigation')[0]
const allEntries = performance.getEntries()
const customMetrics = allEntries
.filter(entry => ['mark', 'measure'].includes(entry.entryType )) // array of PerformanceMark / PerformanceMeasure objects
.reduce((final, current) => { // make an object
final[current.entryType.toUpperCase() + ' — ' + current.name] // key name : "MARK — loadInAsync que/query_grid_webpart start"
= Math.round(
current.entryType === 'measure' ? current.duration : current.startTime
)
@jpvincent
jpvincent / lazy_modal_svelte.jsx
Created October 24, 2023 09:01
Lazy loading svelte component
<script>
let lazyModalComponent;
let isPending = false;
function openModal() {
isPending = true;
lazyModalComponent = import(`./Modal.svelte`);
lazyModalComponent.catch((error) => {
// gestion en cas d'erreur de connexion ici
}).finally(() => {
@jpvincent
jpvincent / lazy_modal_react.jsx
Created October 24, 2023 09:02
lazy load react component
import { lazy, useState, useTransition } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
const Modal = lazy(() => import('./Modal.jsx'));
export default function App() {
const [isPending, startTransition] = useTransition();
const [opened, setOpened] = useState(false);
function openModal() {