🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

adaptive-set

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adaptive-set

Utils for implementing a Set data structure that dynamically adjusts its internal representation based on the number of elements it contains.

0.2.0
latest
Source
npm
Version published
Weekly downloads
283
-43.85%
Maintainers
0
Weekly downloads
 
Created
Source

adaptive-set

Docs Build Status npm-version Coverage Status minified-size

A set of utils for implementing specialized Set data structure designed to optimize memory usage and performance based on the number of elements it contains. It adapts its internal representation to efficiently handle cases where there are no items, a single item, or multiple items.

  • No Items: When the set is empty, it uses an undefined value to represent the absence of elements, minimizing memory usage.
  • Single Item: When the set contains exactly one item, it uses a array [item] to store the single element, providing quick access and iteration.
  • Multiple Items: When the set contains more than one item, it switches to using a standard Set to leverage its efficient handling of multiple elements.

This adaptive approach ensures that the it remains efficient and performant across different usage scenarios, making it an ideal choice for applications where the number of elements can vary significantly.

Install

npm add adaptive-set

Example

import { add, has, remove, size, type AdaptiveSet } from "adaptive-set";

type Fn = () => void;

class MyClass {
  private listeners?: AdaptiveSet<Fn>;

  public notify(): void {
    if (this.listeners) {
      for (const listener of this.listeners) {
        listener();
      }
    }
  }

  public addListener(listener: Fn): void {
    this.listeners = add(this.listeners, listener);
  }

  public removeListener(listener: Fn): void {
    this.listeners = remove(this.listeners, listener);
  }

  public hasListener(listener: Fn): boolean {
    return has(this.listeners, listener);
  }

  public getListenerCount(): number {
    return size(this.listeners);
  }

  public clearListeners(): void {
    this.listeners = undefined;
  }
}

Keywords

adaptive

FAQs

Package last updated on 06 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts