The storefront team wants Consuming for Analytics's figures in Node.js 2,131 . One file runs on either library; only the setup lines differ. A timer (not shown) prints the totals after three quiet seconds:
const lib = process.argv[2] || "confluent"; // "confluent" or "kafkajs"
const { Kafka, logLevel } = lib === "kafkajs" ? require("kafkajs")
: require("@confluentinc/kafka-javascript").KafkaJS;
const config = { brokers: ["localhost:32092"], logLevel: logLevel.ERROR };
const groupId = `booknest-analytics-${lib}`;
const consumer = lib === "kafkajs" ? new Kafka(config).consumer({ groupId })
: new Kafka({ kafkaJS: config }).consumer({ kafkaJS: { groupId, fromBeginning: true } });
...
await consumer.run({ eachBatch: async ({ batch }) => {
biggest = Math.max(biggest, batch.messages.length);
for (const m of batch.messages) {
const kind = m.headers["event-type"]?.toString(); // set by the shop producer
if (!kind) continue;
events++;
if (kind === "order_placed") totals.set(m.key.toString(), JSON.parse(m.value).total);
else if (kind === "order_cancelled") cancelled.add(m.key.toString());
}
last = Date.now();Output
confluent: 390737 events in 6.061 s, batches <= 32 orders 100000, cancelled 5890, kept 3270268.02
The figures match Python's to the cent. On this shared 4-CPU host Node.js took 1.6 times as long as Consuming for Analytics's Python consumer (3.8 s) and 1.9 times as long as Java (The Java Client). Each record becomes a JavaScript object with Buffer fields, handed over at most 32 at a time (js.consumer.max.batch.size; the migration notes claim one). Offsets are auto-committed every 5 seconds; for work that must not repeat, set autoCommit: false and call commitOffsets() after each batch.