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

pipeline:
  processors:
    - nodered_js:
        code: |
          // Your JavaScript code here
          return msg;

Message Format

Messages in Benthos and in the JavaScript processor are handled differently:

In Benthos/Bloblang:

# Message content is the message itself
root = this   # accesses the message content

# Metadata is accessed via meta() function
meta("some_key")   # gets metadata value
meta some_key = "value"   # sets metadata

In JavaScript (Node-RED style):

The processor automatically converts between these formats.

Examples

  1. Pass Through Message Input message:

Metadata:

JavaScript code:

Output: Identical to input

  1. Modify Message Payload Input message:

JavaScript code:

Output message:

  1. Create New Message Input message:

JavaScript code:

Output message:

  1. Drop Messages (Filter) Input messages:

JavaScript code:

Output: Only messages with status "ok" pass through

  1. Working with Metadata Input message:

Metadata:

JavaScript code:

Output message: Same as input

Output metadata:

Equivalent Bloblang:

  1. String Manipulation Input message:

JavaScript code:

Output message:

  1. Numeric Operations Input message:

JavaScript code:

Output message:

  1. 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:

  1. Bloblang is approximately 4-5x faster for simple operations

  2. Bloblang shows more consistent performance (smaller standard deviation)

  3. 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