next-task
Fast microtask queue for all platforms, equivalent rawAsap (based on the ideas and source of rawAsap), but a little faster.
Usage
var nextTask = ; ; console; /** Log: * -> This run sync. * -> Run this async, but "as soon as possible". */ /** Variant with context: */var task = data: ... {/* this === task */}; ;
About rawAsap and microtasks: rawAsap. If you need queue of animation tasks, use raf instead, for synchronize with rendering loop. If you need to perform a long (macrotask) queue of heavy tasks, use setImmediate to give the browser the ability to handle current events. Note that, like rawAsap, next-task does not catch the errors (to work as soon as possible).
Differences from rawAsap
Errors
/** * If a task does throw an error, with rawAsap you need * call requestFlush (after error): */rawAsap; /** * With nextTask just call it without arguments *(or with null or undefined), also after error: */;
Domains
next-task does not support domains for Node.js (rasAsap does).
Promise
next-task uses native Promise, if it is available (only native, and ignores any polyfills). More information: Consider using Promise.prototype.then.
Property 'use'
Property 'use' points to the technology used:
/** In the order of attempts to use: */nextTaskuse === 'setImmediate' || /* only Node.js */ 'Promise' || /* ES6 native promise, if available */ 'MutationObserver' || /* modern browsers */ 'setTimeout' /* all other platforms */
Method 'setCapacity'
Method 'setCapacity' limited the memory usage (more information: function to change rawAsap.capacity value must be added):
nextTask; /* return 1024 */ nextTask; /* return 1024 */nextTask; /* return 1024 */ nextTask; /* return 100 */
Build
Install all the packages from devDependencies in ./node_modules and run build:
$ npm install$ npm run build
Then you will be able to perform tests and benchmark.
Benchmarks
$ npm run benchmark:node$ npm run benchmark:browser
This is benchmark from asap/benchmarks, in which different ways queuing added for comparison. The results are not very stable and not fully explained; for example, a typical result in Node.js:
asap x 5,584 ops/sec ±3.90% rawAsap x 39,700 ops/sec ±7.41% nextTask x 41,186 ops/sec ±7.55% nextTick x 11,711 ops/sec ±5.81% nextTick[] x 23,823 ops/sec ±3.49% setImmediate x 5,860 ops/sec ±4.92% setImmediate[] x 14,182 ops/sec ±3.91% Promise x 811 ops/sec ±3.43% Promise[] x 11,993 ops/sec ±1.89% setTimeout x 612 ops/sec ±1.95% setTimeout[] x 768 ops/sec ±1.32%
The results of benchmark in DOM even more dependent on the browser. For example, in modern Chrome:
asap x 6,770 ops/sec ±4.53% rawAsap x 27,173 ops/sec ±7.02% nextTask x 32,403 ops/sec ±3.36% Promise x 891 ops/sec ±3.33% Promise[] x 10,534 ops/sec ±2.21% MutationObserver[] x 4,792 ops/sec ±5.05% setTimeout x 115 ops/sec ±1.56% setTimeout[] x 226 ops/sec ±1.85%
There "Promise" is run each task by Promise and "Promise[]" means the use of the task queue, so that the whole queue is run with one call Promise; and similarly for the other methods.
Tests
$ npm run test:node$ npm run test:browser