npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

component-map

v1.0.2

Published

Not invasive, performant, and garbage collected storage for React components (and more)

Downloads

5

Readme

Component Map Build Status npm

Not invasive, performant, and garbage collected storage for React components (and more)

ComponentMap allows for most operations WeakMap provides, but it's not a 1:1 shim. In particular, to simplify the implementation it doesn't accept iterable object as a constructor parameter.

ComponentMap it is preconfigured for performant mapping of React classes, but can be easily customized.

ComponentMap promises following:

  • No mutation of provided keys whatsoever (in contrast to most other WeakMap shims)
  • Efficient garbage collection of components if provided with proper WeakMap implementation
  • Decent retrieval performance if no WeakMap is provided

Unfortunately given first promise, it's not possible to prevent memory pollution in case WeakMap is provided. For this reason ComponentMap can be used without WeakMap option only in short-lived sessions, like development or tests.

Usage

Component Map supports subset of WeakMap API.

  • new ComponentMap(options: object) with following options:
    • WeakMap: WeakMap - the WeakMap implementation to use, or undefined, recommended
    • getBucketName: (object) => string - see Configuraton section below, optional
  • #get(key: object): any - returns the value that key corresponds to the key or undefined.
  • #has(key: object): boolean - tells whether there exists a value with given key
  • #set(key: object, value: any) - sets key to given value. The key must be an object.
  • #delete(key: object): boolean - removes the value and returns true if there was a value to delete.

The key option must be an object, by default ComponentMap is optimized to store React components.

Example

class MyComponent { render() { return <div>Hello world</div> } }

const components = new ComponentMap({ WeakMap })
components.set(MyComponent, { metadata: 'something' })
// somewhere else
const meta = components.get(MyComponent).metadata

Configuration

If you wish to use objects other than React components as keys, you can use getBucketName. It allows for specifying custom bucket names. See how ComponentMap is a subclass of BucketMap:

const object = { type: 'MyObject' }
class myMap = new ComponentMap({ getBucketName: (key => key.type) })
myMap.set(MyObject, 'something')

How it works

By default it tries to store components directly into passed WeakMap, if provided. Otherwise it uses custom storage that puts objects into buckets instead of single map, improving retrieval performance a lot. Something like buckets[getBucketName(key)] = value.

ComponentMap doesn't automatically detect WeakMap implementation, but rather allows you to pass it as a constructor parameter. It uses bucketed storage implementation only in case WeakMap is not provided.

License

MIT