@s-ui/performance

1.2.0 • Public • Published

@s-ui/performance

Performance utilities to make your web application go fast ⚡️

Installation

npm install @s-ui/performance

Usage

Delay code execution

Use this function to delay the execution of an expensive operation and prioritize user actions. Keep in mind that it only delays the response by a maximum of 1 frame, an average of 8ms, which is too little for a human to notice for the types of major actions where you’d use this function.

import {delayTask} from '@s-ui/performance';

export default function Example() {
  const [counter, setCounter] = useState(0)

  const handleClick = async () => {
    setCounter(counter => counter + 1)

    await delayTask()

    work() // expensive work
  }

  return <button onClick={handleClick}>{counter}</button>
}

Delay code execution until urgent

Use this function to delay the execution of an expensive operation while the main thread is idle, using requestIdleCallback, and to prioritize user actions. This method ensures the execution is completed before the user leaves the page. It is especially useful for delaying tracking execution.

The delayTaskUntilUrgent function optionally receives an options object. The documentation can be found here (idlefy is used under the hood).

import {delayTaskUntilUrgent} from '@s-ui/performance';

export default function Example() {
  const [counter, setCounter] = useState(0)

  const handleClick = async () => {
    setCounter(counter => counter + 1)

    await delayTaskUntilUrgent()

    track() // expensive work
  }

  return <button onClick={handleClick}>{counter}</button>
}

Dependencies (1)

Dev Dependencies (1)

Package Sidebar

Install

npm i @s-ui/performance

Weekly Downloads

635

Version

1.2.0

License

MIT

Unpacked Size

3.37 kB

Total Files

4

Last publish

Collaborators

  • giodelabarrera.adevinta
  • arnau.guell
  • miriam-gil
  • andresin87.adevinta
  • alfredo.narvaez
  • sendami.luque.ext
  • sebastian.badea.adevinta
  • thomas.page.ext
  • victoria.pasichnyk.ext
  • daniel.perez.ext
  • albert.peiro
  • frandelacasa-adevinta
  • mariapaula.forero.ext
  • hpintos_adevinta
  • sziauberyte
  • victor.perez.adevinta
  • oscar-raig-adevinta
  • carlos.gonzalezl
  • carolina.mallo.ext
  • david.nieto
  • ferran.simon
  • sergi.quintela
  • jamile.radloff
  • xavi_ballestar
  • luz_adv
  • ignacio.rodriguez
  • carlosvillu-adevinta
  • diegomr
  • arturo.vicente
  • adria.velardos
  • emiliovz
  • dann41
  • ruben-martin
  • pol.valls
  • cristina.rodriguez.duque
  • sergio.escano
  • marc.benito
  • azahara
  • patricio.sartore
  • giovanny.sayas.ext
  • david.cuadrado.ext
  • alex.castells
  • beatrizip
  • pablo.rey-adevinta
  • sergiocollado
  • cristhianb
  • alisa_bayanova
  • davidmartin2108
  • ferrangbtw
  • estefania_garcia
  • belen.santos
  • alfredo.arronte
  • joanleon-adv
  • luis-garrido
  • aitor.rodriguez
  • jordi.munoz
  • oscar_ramirez
  • ignacio_navarro
  • a.ferrer
  • gfabregoadv
  • izeller
  • salvador.juan
  • ivanmlaborda
  • alejandro.ferrante
  • pa.chruscinski.ext
  • isabelgomez87
  • jenifer.lopez
  • pablogs
  • javier.miguel
  • oscar.gomez
  • marian.lucaci
  • alverd004
  • oriol.puig
  • nacho_torrella
  • xavi.murcia
  • javiauso
  • alfredo.zimperz
  • francisco.ruiz.lloret
  • andresadv
  • schibstedspain