queue-microtask

中文文档
1.2.3发布于 2021/03/22MITCommonJS零依赖近 30 天下载 5亿

fast, tiny queueMicrotask shim for modern engines

原始使用文档
文档目录12 节
  1. fast, tiny queueMicrotask shim for modern engines
  2. install
  3. usage
  4. What is queueMicrotask and why would one use it?
  5. Who is this package for?
  6. Why not use process.nextTick ?
  7. Why not use setTimeout(fn, 0) ?
  8. Why not use a microtask library like immediate or asap ?
  9. What is a shim?
  10. API
  11. queueMicrotask(fn)
  12. license

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.

版本与发布记录

queue-microtask 共发布 11 个版本,首次发布于 2019/09/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.3latest2021/03/225 年前
1.2.22020/11/245 年前
1.2.12020/11/195 年前
1.2.02020/10/246 年前
1.1.42020/07/216 年前
1.1.32020/06/066 年前

关于 queue-microtask 的常见问题

queue-microtask 是什么?

fast, tiny queueMicrotask shim for modern engines

如何安装 queue-microtask?

在项目目录运行 npm install queue-microtask,或使用 pnpm add / yarn add / bun add。

queue-microtask 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/queue-microtask 是否存在。

queue-microtask 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

queue-microtask 可以商用吗?

queue-microtask 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

queue-microtask 的下载量有多少?

根据 npm 官方下载统计,queue-microtask 在 2026/10/05 – 2026/11/03 这 30 天内被下载 504,836,549 次,日均约 16,827,885 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

queue-microtask 还在维护吗?

最新版本 1.2.3 发布于 2021/03/22(5 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。