• Stars
    star
    172
  • Rank 213,389 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

fast, tiny `queueMicrotask` shim for modern engines

queue-microtask ci 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.

More Repositories

1

simple-peer

๐Ÿ“ก Simple WebRTC video, voice, and data channels
JavaScript
7,206
star
2

SpoofMAC

๐Ÿ’ผ Change your MAC address for debugging
Python
2,988
star
3

thanks

๐Ÿ™Œ Give thanks to the open source maintainers you depend on! โœจ
JavaScript
2,768
star
4

spoof

Easily spoof your MAC address in macOS, Windows, & Linux!
JavaScript
1,745
star
5

buffer

The buffer module from node.js, for the browser.
JavaScript
1,703
star
6

awesome-mad-science

Delightful npm packages that make you say "wow, didn't know that was possible!"
1,089
star
7

filldisk.com

๐Ÿ’พ Masterful trolling with HTML5 localStorage
HTML
910
star
8

hostile

Simple, programmatic `/etc/hosts` manipulation (in node.js)
JavaScript
763
star
9

TheAnnoyingSite.com

The Annoying Site a.k.a. "The Power of the Web Platform"
JavaScript
727
star
10

yt-player

Simple, robust, blazing-fast YouTube Player API
JavaScript
662
star
11

clipboard-copy

Lightweight copy to clipboard for the web
JavaScript
585
star
12

bitmidi.com

๐ŸŽน Listen to free MIDI songs, download the best MIDI files, and share the best MIDIs on the web
JavaScript
507
star
13

electron-workshop

Workshop: Build cross-platform desktop apps with Electron
JavaScript
491
star
14

drag-drop

HTML5 drag & drop for humans
JavaScript
486
star
15

simple-get

Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines
JavaScript
396
star
16

md5-password-cracker.js

Crack MD5 passwords with JavaScript Web Workers
JavaScript
381
star
17

run-parallel

Run an array of functions in parallel
JavaScript
363
star
18

magickeyboard.io

Ultimate hacker keyboard
JavaScript
339
star
19

safe-buffer

Safer Node.js Buffer API
JavaScript
338
star
20

timidity

Play MIDI files in the browser w/ Web Audio, WebAssembly, and libtimidity
Shell
315
star
21

p2p-graph

Real-time P2P network visualization with D3
JavaScript
286
star
22

multistream

A stream that emits multiple other streams one after another (streams3)
JavaScript
283
star
23

zelda

Automatically `npm link` all your packages together!
JavaScript
280
star
24

run-series

Run an array of functions in series
JavaScript
240
star
25

funding

Let's get open source maintainers paid โœจ
JavaScript
209
star
26

render-media

Intelligently render media files in the browser
JavaScript
199
star
27

cs253.stanford.edu

CS 253 Web Security course at Stanford University
JavaScript
194
star
28

simple-websocket

Simple, EventEmitter API for WebSockets
JavaScript
183
star
29

last-fm

Simple, robust LastFM API client (for public data)
JavaScript
169
star
30

studynotes.org

โœ๏ธ Learn faster. Study better.
JavaScript
159
star
31

whiteboard

P2P Whiteboard powered by WebRTC and WebTorrent
JavaScript
154
star
32

ytinstant.com

Real-time YouTube video surfing.
JavaScript
148
star
33

mediasource

MediaSource API as a node.js Writable stream
JavaScript
136
star
34

chrome-net

Use the Node `net` API in Chrome Apps
JavaScript
135
star
35

login-with-twitter

Login with Twitter. OAuth without the nonsense.
JavaScript
122
star
36

cross-zip

Cross-platform .zip file creation
JavaScript
120
star
37

Fullscreen-API-Attack

Demo of phishing attack on the native HTML5 full screen API.
JavaScript
116
star
38

capture-frame

Capture video screenshot from a `<video>` tag (at the current time)
JavaScript
113
star
39

ieee754

Read/write IEEE754 floating point numbers from/to a Buffer or array-like object.
JavaScript
111
star
40

cyberhobo

Offline `git push` and `npm publish` for cyberhobos
JavaScript
111
star
41

Instant.fm

Share music playlists with friends.
JavaScript
108
star
42

unmute-ios-audio

Enable/unmute WebAudio on iOS, even while mute switch is on
JavaScript
106
star
43

available

Scan npm for available package names
JavaScript
100
star
44

bg-sound

Web Component to emulate the old-school <bgsound> HTML element
JavaScript
95
star
45

lxjs-chat

Talk to strangers! (P2P video chat using WebRTC)
JavaScript
94
star
46

play.cash

๐ŸŽถ Music lovers, rejoice.
JavaScript
93
star
47

reddit

Simple Reddit API client
JavaScript
90
star
48

is-buffer

Determine if an object is a Buffer
JavaScript
89
star
49

run-waterfall

Run an array of functions in series, each passing its results to the next function
JavaScript
89
star
50

run-auto

Determine the best order for running async functions, LIKE MAGIC!
JavaScript
86
star
51

WireSheep

WireSheep shows you each user on the network and all the HTTP requests they're making in a pretty News Feed, a la Facebook.
C++
83
star
52

oculus-drone

Pilot a Parrot AR Drone with the Oculus Rift virtual reality headset!
C
80
star
53

string-to-stream

Convert a string into a stream (streams2)
JavaScript
79
star
54

config

Server config files (nginx, mysql, certbot)
HTML
78
star
55

feross.org

Pure concentrated awesome (a.k.a. my blog)
HTML
78
star
56

run-parallel-limit

Run an array of functions in parallel, but limit the number of tasks executing at the same time
JavaScript
76
star
57

arch

Better `os.arch()` for node and the browser -- detect OS architecture
JavaScript
74
star
58

blob-to-buffer

Convert a Blob to a Buffer.
JavaScript
72
star
59

CMSploit

Security scanner to find temporary config files that contain passwords on public websites
CoffeeScript
70
star
60

location-history

Lightweight browser location history abstraction
JavaScript
69
star
61

fromentries

Object.fromEntries() ponyfill (in 6 lines)
JavaScript
66
star
62

typedarray-to-buffer

Convert a typed array to a Buffer without a copy.
JavaScript
65
star
63

express-sitemap-xml

Serve sitemap.xml from a list of URLs in Express
JavaScript
63
star
64

speakeasyjs.com

The JavaScript meetup for ๐Ÿฅผ mad science, ๐Ÿง™โ€โ™‚๏ธ hacking, and ๐Ÿงช experiments
JavaScript
63
star
65

beepbeep

Make a console beep sound.
JavaScript
62
star
66

connectivity

Detect if the network is up (do we have connectivity?)
JavaScript
60
star
67

stream-to-blob

Convert a Readable Stream to a Blob
JavaScript
59
star
68

color-scheme-change

Detect system color scheme changes on the web (Dark Mode)
JavaScript
58
star
69

conferences

Conferences that I will attend or have already attended
53
star
70

Life

An experiment in treating life like a software project.
50
star
71

git-pull-or-clone

Ensure a git repo exists on disk and that it's up-to-date
JavaScript
50
star
72

Facebook-Like-Everything

Bookmarklet to Like every post+comment that you see on Facebook.
JavaScript
47
star
73

load-script2

Dynamic script loading for modern browsers
JavaScript
46
star
74

cctv.js

Watch live visitors using your website.
JavaScript
46
star
75

simple-sha256

Generate SHA-256 hashes (in Node and the Browser)
JavaScript
45
star
76

async-lru

A simple async LRU cache supporting O(1) set, get and eviction of old keys
JavaScript
45
star
77

peerdb

JavaScript
44
star
78

dotfiles

Configuration files for zsh, screen, git, ssh, sublime, dot dot dot
Shell
39
star
79

chrome-dgram

Use the Node `dgram` API in Chrome Apps
JavaScript
39
star
80

webcam-spy

Demo of Adobe Flash clickjacking vulnerability to spy on a user's webcam.
JavaScript
39
star
81

standard-react

JavaScript Standard Style for React Users
JavaScript
36
star
82

BrainGrinder

Instant foreign language flashcards (with audio!)
CSS
34
star
83

ahh-windows

Windows XP Emulator -- in the browser :)
HTML
34
star
84

chrome-dns

Use the Node `dns` API in Chrome Apps
JavaScript
34
star
85

objection-slug

Automatically generate slugs for an Objection.js model
JavaScript
33
star
86

call-log

Instrument a JavaScript class (or object) so that anytime a method function is called it gets logged to the console.
JavaScript
33
star
87

zero-fill

Zero-fill a number to the given size.
JavaScript
31
star
88

caught-in-a-web-of-apis

Attack code to accompany "Caught in a Web of APIs: An Analysis of Powerful Web APIs" research paper
JavaScript
30
star
89

cache-chunk-store

In-memory LRU (least-recently-used) cache for abstract-chunk-store compliant stores
JavaScript
29
star
90

clash

A Simple Bash-Like Shell
C++
28
star
91

preload-img

Preload an image on a webpage
JavaScript
26
star
92

chunk-store-stream

Convert an abstract-chunk-store compliant store into a readable or writable stream
JavaScript
26
star
93

feross-card

It's me, Feross!
JavaScript
24
star
94

vlc-command

Find VLC player command line path
JavaScript
24
star
95

raft

An understandable consensus algorithm
C++
23
star
96

simple-concat

Super-minimalist version of `concat-stream`. Less than 15 lines!
JavaScript
23
star
97

stream-to-blob-url

Convert a Readable Stream to a Blob URL
JavaScript
23
star
98

SuperTranslate

"Super translate" words into many different languages at once
CSS
21
star
99

Fling

Send songs, videos, web urls from your phone to your desktop with a flick of your wrist
Objective-C
21
star
100

detect-proxy

Using <img> to detect whether the user is browsing through a proxy or not.
21
star