Giter Club home page Giter Club logo

shared-ring-buffer's Introduction

shared-ring-buffer

Overview

The shared-ring-buffer library is designed to streamline data transfer between multiple workers or threads in JavaScript applications. It leverages a ring buffer structure for continuous data reads and writes, offering a performance boost over the traditional postMessage() method. This approach simplifies development by abstracting complex behaviors like wrap-around, making it more accessible for developers to implement efficient data sharing across threads.

Prerequisite

To use shared-ring-buffer, your environment must have https enabled, and your web page must include the following headers for security and cross-origin policies:

'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',

Supported buffer views

The library supports a wide range of typed arrays for versatile data handling:

  • Uint8Array
  • Uint8ClampedArray
  • Int8Array
  • Uint16Array
  • Int16Array
  • Uint32Array
  • Int32Array
  • Float32Array
  • Float64Array

Why shared-ring-buffer?

shared-ring-buffer is meant for developing high-performance JavaScript applications with responsive UIs. Offloading tasks to Web Workers can improve application responsiveness, but traditional communication via postMessage() can be slow and inconsistent. Modern browsers offer the SharedArrayBuffer API to facilitate shared memory access across threads. shared-ring-buffer simplifies this process by providing a straightforward interface for safe, race condition-free data reading and writing, thereby enhancing developer productivity and application performance.

API

declare function isSupportedSharedArrayBuffer(): boolean;

declare function createRingBuffer<T extends TypedArray>(
    elementCount: number,
    typedArrayConstructor?: TypedArrayConstructor,
    bufferConstructor?: { new (byteLength: number): ArrayBufferLike; }
): RingBuffer<T> | Error;

declare class RingBuffer<T extends TypedArray> {
    constructor(buffer: ArrayBufferLike, typedArrayConstructor: TypedArrayConstructor);
    getBuffer: () => ArrayBuffer;
    shift(size: number): T;
    shiftAndCopy(outputBuffer: T): T;
    push(data: T): true | Error;
    getCapacity(): number;
    getAvailableSize(): number;
    getOccupiedSize(): number;
}

Usage

To effectively utilize the shared-ring-buffer in your project, start by creating a ring buffer once with createRingBuffer. Then, extract its raw buffer using getBuffer() and transfer it to another thread. In the receiving thread, instantiate a new RingBuffer with this raw buffer.

Following example is taken from audio processing code where Float32Array represents audio samples.

// In the main thread
const worker = new Worker("worker.js");

const ringBuffer = createRingBuffer<Float32Array>(500_000, Float32Array);
worker.postMessage({
    sharedBuffer: ringBuffer.getBuffer(),
});
ringBuffer.push(new Float32Array(1_000)); // pushes 1000 empty audio samples to buffer

// In an audio worker
self.onmessage = (e: MessageEvent) => {
    const ringBuffer = new RingBuffer<Float32Array>(e.data.sharedBuffer, Float32Array);
    const data = ringBuffer.shift(1_000)); // reads 1000 audio samples from buffer
}

Installation

npm install shared-ring-buffer

Run examples

npm run dev

Test

npm run test

GIT

https://github.com/guntisdev/shared-ring-buffer

License

shared-ring-buffer is MIT licensed.

shared-ring-buffer's People

Watchers

Guntis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.