Simple JavaScript Speedometer

1.1.0 · maintenance · verified Sun Apr 19

Speedometer is a lightweight JavaScript utility designed for measuring throughput in bytes per second over a sliding time window. It is particularly useful in Node.js environments for tracking data transfer rates, such as file streaming or network activity. The current stable version is 1.1.0, and the package appears to be in a maintenance state, with infrequent updates primarily for tooling rather than new features or significant API changes. Its key differentiator lies in its extreme simplicity and low-level focus, providing a raw numerical measurement without UI components, contrasting with many other 'speedometer' packages that offer graphical gauges for web frameworks. Users can configure the buffer duration for averaging speed, which defaults to 5 seconds, allowing for flexible measurement intervals.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize the speedometer, feed it data chunk lengths, and continuously log the calculated bytes per second for a data stream.

const speedometer = require('speedometer');
const fs = require('fs');

// Create a speedometer instance with a 10-second buffer
const speed = speedometer(10);
const stream = fs.createReadStream('/dev/urandom', { highWaterMark: 1024 * 1024 }); // Read in 1MB chunks

let totalBytes = 0;
let lastLogTime = Date.now();

stream.on('data', function(data) {
  // Pass the amount of bytes transferred to the speedometer
  const bytesPerSecond = speed(data.length);
  totalBytes += data.length;

  // Log speed every second
  if (Date.now() - lastLogTime >= 1000) {
    console.log(`Current speed: ${bytesPerSecond.toFixed(2)} bytes/second`);
    lastLogTime = Date.now();
  }
});

stream.on('end', () => {
  console.log(`
Finished reading. Total bytes: ${totalBytes}`);
  const finalSpeed = speed(); // Get current speed one last time
  console.log(`Final calculated speed (over buffer): ${finalSpeed.toFixed(2)} bytes/second`);
});

stream.on('error', (err) => {
  console.error('Stream error:', err);
});

view raw JSON →