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

search2d

v0.0.11

Published

Quickly search for entities in the 2D world with a rectangular range query.

Downloads

32

Readme

Search 2D

search2d-concept

This package is under development.

Quickly search for entities in the 2D world with a rectangular range query.

Install

npm i search2d

Example

import { EntityPosition, Search2D, SearchableEntity } from "search2d";

type ExampleEntityObject = {
    name: string; // Additional field
} & SearchableEntity;

// If you want to use a class, write the following
class ExampleEntityClass implements SearchableEntity {
    constructor(
        readonly id: string, // Required
        readonly position: EntityPosition, // Required
        readonly name: string, // Additional field
    ) {}
}

// Specify field height and width
// Entity's position range: 0 <= y <= height,  0 <= x <= width
const search = new Search2D<ExampleEntityObject>({ height: 100, width: 100 });

const entity: ExampleEntityObject = {
    id: "001", // id must be unique
    position: new EntityPosition({ x: 10, y: 20 }),
    name: "buri",
};
// Register entity to search
search.register(entity);

// Search by query
const result = search.search({
    position: {
        xFrom: 10,
        yFrom: 10,
        xTo: 20,
        yTo: 20,
    },
});
console.log(result);
// {
//   entities: [ { id: '001', position: [EntityPosition], name: 'buri' } ]
// }

// Move entity position (Change x, y at the same time, it is faster)
entity.position.set({ x: 15, y: 15 });

// x, y can also be set individually
entity.position.x = 15;
entity.position.y = 15;

// You can deregister entity
search.deregister(entity);

// Deregister all entities before disposing search2D instance to prevent memory leak.
search.deregisterAll();

Benchmark

Search2D is about 10x faster than NaiveSearch. (10k entities)
benchmark code is here

| | NaiveSearch | Search2D | |----------------|-------------|----------| | Registration | 3ms | 17ms | | Deregistration | 3ms | 9ms | | Search | 1706ms | 174ms |

// NaiveSearch (https://github.com/buri83/search2d/blob/main/src/naiveSearch.ts)
const entities: T[] = [];
for (const entity of this.entities.values()) {
    const isContained =
        query.position.xFrom <= entity.position.x &&
        entity.position.x <= query.position.xTo &&
        query.position.yFrom <= entity.position.y &&
        entity.position.y <= query.position.yTo;
    if (isContained) {
        entities.push(entity);
    }
}