Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adaptive-set

Package Overview
Dependencies
Maintainers
1
Versions
4
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.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
175
increased by19.86%
Maintainers
1
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

FAQs

Package last updated on 28 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc