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

n-set

v2.0.1

Published

Allows to put arrays into sets. Works similarly like ES6 Sets.

Downloads

14

Readme

n-set

Build Status Coverage Status license

Allows to put arrays into sets. Works similarly like ES6 Sets.

Contains type definitions for TypeScript.

Install

npm i n-set

Example

const NSet = require('n-set');
const set = new NSet();
set.add(1);
set.add(2);
set.add(2);
console.log(set); // NSet { (1), (2) }
set.add(1, 2, 3);
set.add(1, 5);
set.add([1, 5]); // you can pass it as an array too
console.log(set); // NSet { (1), (1, 2, 3), (1, 5), (2) }
set.delete(1, 2, 3);
console.log(set); // NSet { (1), (1, 5), (2) }
console.log(set.has(1, 2, 3)); // false
console.log(set.has(1, 5)); // true

How it works?

It uses ES6 Maps for storing each level of depth and an ES6 Set for the last one.

By example for storing (1, 2, 3) and (1, 2, 4), it will use internally this data structure:

Map( 1 => 
  Map ( 2 => 
    Set ( 3, 4 )
  )
)

Methods

set.add(tuple)

Appends the specified tuple of values to the end of Set. The values can be passed as an array or as parameters: set.add(1, 2, 3) has the same behaviour as set.add([1, 2, 3]).

Note: Adding undefined value is not supported.

Returns the Set object.

set.clear()

Removes all elements from a Set object.

set.delete(tuple)

Removes the specified tuple from a Set object. The values can be passed as an array or as parameters: set.delete(1, 2, 3) has the same behaviour as set.delete([1, 2, 3]). Returns true if an element in the Set object has been removed successfully; otherwise false.

set.entries()

Returns a new Iterator object that contains an array of [tuple, tuple] for each element in the Set object.

set.forEach(func)

Executes the provided function once for each value in the Set object.

set.has(tuple)

Returns a boolean indicating whether an element with the specified tuple exists in a Set object or not.

set.size

Returns the number of elements in the Set object.

set.values()

Returns a new Iterator object that contains the values for each element in the Set object.

Performance

Each test consists of three operations:

  • .add() 250 000 tuples
  • .has() on the 250 000 tuples
  • .delete() on the 250 000 tuples

1-depth has ~250 000 tuples of length 1. [v1] - Native ES6 Sets and Maps supports only these.

2-depth has ~250 000 tuples of length 2. [v1, v2]

3-depth has ~250 000 tuples of length 3. [v1, v2, v3]

4-depth has ~250 000 tuples of length 4. [v1, v2, v3, v4]

The source of this benchmark can be found in file benchmark.js.

Running it with Node.js v8.9.4 on Intel i7-7700K CPU.

After 5 runs:
------------
Map 1-depth  - Min: 23.359 ms, Max: 27.608 ms, Mean: 25.142 ms, Median: 23.847 ms
Set 1-depth  - Min: 22.383 ms, Max: 27.031 ms, Mean: 24.195 ms, Median: 24.385 ms
NSet 1-depth - Min: 41.212 ms, Max: 172.270 ms, Mean: 70.170 ms, Median: 41.787 ms
NSet 2-depth - Min: 97.894 ms, Max: 110.139 ms, Mean: 102.154 ms, Median: 110.139 ms
NSet 3-depth - Min: 206.468 ms, Max: 349.996 ms, Mean: 237.171 ms, Median: 208.478 ms
NSet 4-depth - Min: 972.355 ms, Max: 1079.711 ms, Mean: 1052.376 ms, Median: 1072.435 ms

License

MIT