
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
parallel-transform
Advanced tools
Transform stream that allows you to run your transforms in parallel without changing the order
The parallel-transform npm package is a Node.js module that allows you to perform parallel transformations on streams of data. It is particularly useful when you need to process data in a stream without overloading the system with too many concurrent operations. It maintains the order of the stream and allows you to specify the maximum number of concurrent transformations.
Parallel Stream Transformation
This feature allows you to create a transform stream that can process data in parallel. The code sample shows how to create a parallel transform stream that converts input data to uppercase with a maximum of 10 concurrent operations.
const ParallelTransform = require('parallel-transform');
const stream = require('stream');
const parallelStream = new ParallelTransform(10, function(data, callback) {
// Perform some asynchronous operation
setTimeout(() => {
callback(null, data.toString().toUpperCase());
}, 100);
});
process.stdin.pipe(parallelStream).pipe(process.stdout);
through2-concurrent is similar to parallel-transform in that it allows you to process streams of data concurrently. It provides a simple API to create a transform stream with concurrency control. The main difference is that through2-concurrent is built on top of through2, which is a thin wrapper around Node.js streams.Transform.
async-transform is another package that offers similar functionality to parallel-transform. It allows for asynchronous transformation of stream data with concurrency control. However, it differs in its API and the way it handles the transformation function, which is based on the async module.
stream-each provides a way to iterate over the data in a stream and apply an asynchronous function to each chunk of data. While it does not directly create a transform stream, it can be used in combination with other stream methods to achieve parallel processing. It is more focused on iteration rather than transformation.
Transform stream for Node.js that allows you to run your transforms in parallel without changing the order of the output.
npm install parallel-transform
It is easy to use
var transform = require('parallel-transform');
var stream = transform(10, function(data, callback) { // 10 is the parallism level
setTimeout(function() {
callback(null, data);
}, Math.random() * 1000);
});
for (var i = 0; i < 10; i++) {
stream.write(''+i);
}
stream.end();
stream.on('data', function(data) {
console.log(data); // prints 0,1,2,...
});
stream.on('end', function() {
console.log('stream has ended');
});
If you run the above example you'll notice that it runs in parallel (does not take ~1 second between each print) and that the order is preserved
All transforms are Node 0.10 streams. Per default they are created with the options {objectMode:true}
.
If you want to use your own stream options pass them as the second parameter
var stream = transform(10, {objectMode:false}, function(data, callback) {
// data is now a buffer
callback(null, data);
});
fs.createReadStream('filename').pipe(stream).pipe(process.stdout);
Passing the option {ordered:false}
will output the data as soon as it's processed by a transform, without waiting to respect the order.
MIT
FAQs
Transform stream that allows you to run your transforms in parallel without changing the order
The npm package parallel-transform receives a total of 3,272,494 weekly downloads. As such, parallel-transform popularity was classified as popular.
We found that parallel-transform demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.