All Projects → feross → Queue Microtask

feross / Queue Microtask

Licence: mit
fast, tiny `queueMicrotask` shim for modern engines

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Queue Microtask

Perf Monitor
Performance monitor. Simple UI component that helps you measure performance.
Stars: ✭ 115 (-7.26%)
Mutual labels:  browser
Node Blockly
Blockly for Node.js and Browser via CommonJS module
Stars: ✭ 117 (-5.65%)
Mutual labels:  browser
Wendigo
A proper monster for front-end automated testing
Stars: ✭ 121 (-2.42%)
Mutual labels:  browser
Slouch
A JS client for CouchDB that does the heavy lifting
Stars: ✭ 116 (-6.45%)
Mutual labels:  browser
Kosmonaut
A web browser engine for the space age 🚀
Stars: ✭ 1,531 (+1134.68%)
Mutual labels:  browser
Marionette
Selenium alternative for Crystal. Browser manipulation without the Java overhead.
Stars: ✭ 119 (-4.03%)
Mutual labels:  browser
Anti Webminer
Anti-WebMiner protects your PC against web cryptocurrency miners (JS scripts like Coinhive executed in the browser) by modifying Windows hosts file
Stars: ✭ 114 (-8.06%)
Mutual labels:  browser
Foot traffic
Pure Ruby DSL for Chrome scripting based on Ferrum. No Selenium required. Works from any script. Simulate web app usage scenarios in production or locally.
Stars: ✭ 123 (-0.81%)
Mutual labels:  browser
Cyb Archeology
🌎 Personal immortal robot for the The Great Web
Stars: ✭ 117 (-5.65%)
Mutual labels:  browser
Container Query
A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.
Stars: ✭ 119 (-4.03%)
Mutual labels:  browser
Sushi Browser
Sushi Browser is the next generation browser which mounts the multi-panel and the video support function and so on. Its goal is to be as fantastic as sushi. 🍣
Stars: ✭ 116 (-6.45%)
Mutual labels:  browser
Vertical Tabs Reloaded
Firefox add-on for arranging tabs vertically
Stars: ✭ 115 (-7.26%)
Mutual labels:  browser
Onionbrowser
An open-source, privacy-enhancing web browser for iOS, utilizing the Tor anonymity network
Stars: ✭ 1,702 (+1272.58%)
Mutual labels:  browser
Seelog
简单易用 浏览器实时查看日志
Stars: ✭ 116 (-6.45%)
Mutual labels:  browser
Rpg
Online Role Playing Game (based on Laravel)
Stars: ✭ 121 (-2.42%)
Mutual labels:  browser
Browser Metrics
A collection of metrics tools for measuring performance ⚡️
Stars: ✭ 115 (-7.26%)
Mutual labels:  browser
Abstract Leveldown
An abstract prototype matching the leveldown API
Stars: ✭ 118 (-4.84%)
Mutual labels:  browser
Marinara
Pomodoro® time management assistant for Chrome
Stars: ✭ 1,806 (+1356.45%)
Mutual labels:  browser
Docx
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
Stars: ✭ 2,150 (+1633.87%)
Mutual labels:  browser
Librefox
License: Mozilla Public License 2.0
Stars: ✭ 1,574 (+1169.35%)
Mutual labels:  browser

queue-microtask travis npm downloads javascript style guide

fast, tiny queueMicrotask shim for modern engines

  • Use queueMicrotask in all modern JS engines.
  • No dependencies. Less than 10 lines. No shims or complicated fallbacks.
  • Optimal performance in all modern environments
    • Uses queueMicrotask in modern environments
    • Fallback to Promise.resolve().then(fn) in Node.js 10 and earlier, and old browsers (same performance as queueMicrotask)

install

npm install queue-microtask

usage

const queueMicrotask = require('queue-microtask')

queueMicrotask(() => { /* this will run soon */ })

What is queueMicrotask and why would one use it?

The queueMicrotask function is a WHATWG standard. It queues a microtask to be executed prior to control returning to the event loop.

A microtask is a short function which will run after the current task has completed its work and when there is no other code waiting to be run before control of the execution context is returned to the event loop.

The code queueMicrotask(fn) is equivalent to the code Promise.resolve().then(fn). It is also very similar to process.nextTick(fn) in Node.

Using microtasks lets code run without interfering with any other, potentially higher priority, code that is pending, but before the JS engine regains control over the execution context.

See the spec or Node documentation for more information.

Who is this package for?

This package allows you to use queueMicrotask safely in all modern JS engines. Use it if you prioritize small JS bundle size over support for old browsers.

If you just need to support Node 12 and later, use queueMicrotask directly. If you need to support all versions of Node, use this package.

Why not use process.nextTick?

In Node, queueMicrotask and process.nextTick are essentially equivalent, though there are subtle differences that don't matter in most situations.

You can think of queueMicrotask as a standardized version of process.nextTick that works in the browser. No need to rely on your browser bundler to shim process for the browser environment.

Why not use setTimeout(fn, 0)?

This approach is the most compatible, but it has problems. Modern browsers throttle timers severely, so setTimeout(…, 0) usually takes at least 4ms to run. Furthermore, the throttling gets even worse if the page is backgrounded. If you have many setTimeout calls, then this can severely limit the performance of your program.

Why not use a microtask library like immediate or asap?

These packages are great! However, if you prioritize small JS bundle size over optimal performance in old browsers then you may want to consider this package.

This package (queue-microtask) is four times smaller than immediate, twice as small as asap, and twice as small as using process.nextTick and letting the browser bundler shim it automatically.

Note: This package throws an exception in JS environments which lack Promise support -- which are usually very old browsers and Node.js versions.

Since the queueMicrotask API is supported in Node.js, Chrome, Firefox, Safari, Opera, and Edge, the vast majority of users will get optimal performance. Any JS environment with Promise, which is almost all of them, also get optimal performance. If you need support for JS environments which lack Promise support, use one of the alternative packages.

What is a shim?

In computer programming, a shim is a library that transparently intercepts API calls and changes the arguments passed, handles the operation itself or redirects the operation elsewhere. – Wikipedia

This package could also be described as a "ponyfill".

A ponyfill is almost the same as a polyfill, but not quite. Instead of patching functionality for older browsers, a ponyfill provides that functionality as a standalone module you can use. – PonyFoo

API

queueMicrotask(fn)

The queueMicrotask() method queues a microtask.

The fn argument is a function to be executed after all pending tasks have completed but before yielding control to the browser's event loop.

license

MIT. Copyright (c) Feross Aboukhadijeh.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].