Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Package description
The asap npm package is designed for scheduling tasks to execute as soon as possible, but not before the current stack has cleared. This is particularly useful for optimizing performance and responsiveness in applications by ensuring that heavy tasks do not block the event loop, allowing the UI and other scripts to run smoothly.
Task scheduling
This feature allows you to schedule tasks that will run as soon as possible, but after the current call stack has cleared. It's useful for deferring expensive operations so that they don't block the UI or other high-priority tasks.
require('asap')(function() {
console.log('This runs as soon as possible, but not before this script.');
});
This is a Node.js specific feature, not an npm package, that allows you to queue a function to be executed at the next iteration of the event loop. It's similar to asap in that it defers execution, but it's built into Node.js and not suitable for browser environments.
setImmediate is another Node.js feature with a polyfill for browsers that schedules a task to run after the current event loop cycle. It's similar to asap but is a native API and not a package. It tends to be used in scenarios where compatibility across environments is important.
While not directly offering the same functionality, promise-polyfill provides a way to use Promises in environments that do not support them natively. Since Promises can be used to schedule tasks asynchronously, this package indirectly competes with asap in terms of providing asynchronous capabilities.
Readme
This asap
CommonJS package contains a single asap
module that
exports a single asap
function that executes a function as soon as
possible.
asap(function () {
// ...
});
More formally, ASAP provides a fast event queue that will execute tasks until it is empty before yielding to the JavaScript engine's underlying event-loop. When the event queue becomes non-empty, ASAP schedules a flush event, preferring for that event to occur before the JavaScript engine has an opportunity to perform IO tasks or rendering, thus making the first task and subsequent tasks semantically indistinguishable. ASAP uses a variety of techniques to preserve this invariant on different versions of browsers and NodeJS.
By design, ASAP can starve the event loop on the theory that, if there is enough work to be done synchronously, albeit in separate events, long enough to starve input or output, it is a strong indicator that the program needs to push back on scheduling more work.
Take care. ASAP can sustain infinite recursive calls indefinitely without warning. This is behaviorally equivalent to an infinite loop. It will not halt from a stack overflow, but it will chew through memory (which is an oddity I cannot explain at this time). Just as with infinite loops, you can monitor a Node process for this behavior with a heart-beat signal. As with infinite loops, a very small amount of caution goes a long way to avoiding problems.
function loop() {
asap(loop);
}
loop();
ASAP is distinct from setImmediate
in that it does not suffer the
overhead of returning a handle and being possible to cancel. For a
setImmediate
shim, consider setImmediate.
If a task throws an exception, it will not interrupt the flushing of high-priority tasks. The exception will be postponed to a later, low-priority event to avoid slow-downs, when the underlying JavaScript engine will treat it as it does any unhandled exception.
ASAP has been factored out of the Q asynchronous promise library.
It originally had a naïve implementation in terms of setTimeout
, but
Malte Ubl provided an insight that postMessage
might be
useful for creating a high-priority, no-delay event dispatch hack.
Since then, Internet Explorer proposed and implemented setImmediate
.
Robert Kratić began contributing to Q by measuring the performance of
the internal implementation of asap
, paying particular attention to
error recovery. Domenic, Robert, and I collectively settled on the
current strategy of unrolling the high-priority event queue internally
regardless of what strategy we used to dispatch the potentially
lower-priority flush event. Domenic went on to make ASAP cooperate with
NodeJS domains.
For further reading, Nicholas Zakas provided a thorough article on The Case for setImmediate.
Copyright 2009-2013 by Contributors MIT License (enclosed)
FAQs
High-priority task queue for Node.js and browsers
The npm package asap receives a total of 14,562,171 weekly downloads. As such, asap popularity was classified as popular.
We found that asap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.