Node-RED JavaScript Processor
The Node-RED JavaScript processor allows you to write JavaScript code to process messages in a style similar to Node-RED function nodes. This makes it easy to port existing Node-RED functions to Benthos or write new processing logic using familiar JavaScript syntax.
Use the nodered_js
processor instead of the tag_processor
when you need full control over the payload and require custom processing logic that goes beyond standard tag or time series data handling. This processor allows you to write custom JavaScript code to manipulate both the payload and metadata, providing the flexibility to implement complex transformations, conditional logic, or integrate with other systems.
Configuration
Message Format
Messages in Benthos and in the JavaScript processor are handled differently:
In Benthos/Bloblang:
In JavaScript (Node-RED style):
The processor automatically converts between these formats.
Examples
Pass Through Message Input message:
Metadata:
JavaScript code:
Output: Identical to input
Modify Message Payload Input message:
JavaScript code:
Output message:
Create New Message Input message:
JavaScript code:
Output message:
Drop Messages (Filter) Input messages:
JavaScript code:
Output: Only messages with status "ok" pass through
Working with Metadata Input message:
Metadata:
JavaScript code:
Output message: Same as input
Output metadata:
Equivalent Bloblang:
String Manipulation Input message:
JavaScript code:
Output message:
Numeric Operations Input message:
JavaScript code:
Output message:
Logging Input message:
Metadata:
JavaScript code:
Output: Same as input, with log messages in Benthos logs
Performance Comparison
When choosing between Node-RED JavaScript and Bloblang for message processing, consider the performance implications. Here's a benchmark comparison of both processors performing a simple operation (doubling a number) on 1000 messages:
JavaScript Processing:
Median: 15.4ms
Mean: 20.9ms
Standard Deviation: 9.4ms
Range: 13.8ms - 39ms
Bloblang Processing:
Median: 3.7ms
Mean: 4ms
Standard Deviation: 800µs
Range: 3.3ms - 5.6ms
Key Observations:
Bloblang is approximately 4-5x faster for simple operations
Bloblang shows more consistent performance (smaller standard deviation)
However, considering typical protocol converter workloads (around 1000 messages/second), the performance difference is negligible for most use cases. The JavaScript processor's ease of use and familiarity often outweigh the performance benefits of Bloblang, especially for smaller user-generated flows.
Note that these benchmarks represent a simple operation. The performance difference may vary with more complex transformations or when using advanced JavaScript features.
Last updated