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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ecsjs

v1.4.0

Published

An entity component system library for JavaScript

Readme

Entity Component System for Javascript

Pipeline status NPM version jsDelivr version NPM downloads

BuyMeACoffee

An entity component system library for JavaScript.

Table of Contents

Features

  • Simple, ID-based Entities: Entities are treated as lightweight numeric identifiers, making them easy to track and manage without the overhead of complex objects.
  • Component-Driven Data: Define your data using simple classes that can be dynamically attached to, or removed from, any entity at runtime.
  • Flexible Entity Management: Use the built-in global instance (ecs) or create isolated EntityMap managers, providing get, set, remove and more api functions.
  • Iterable Queries: Retrieve and filter sets of entities by primary component keys and relationships.
  • Logic-Data Decoupling: Designed to support independent systems that process data in bulk, keeping your logic clean and separate from your state.
  • High-Performance Backbone: A minimalist, zero-dependency core optimized for high-frequency updates and large-scale entity management.

Install

npm install --save ecsjs

Examples

Usage

Browser

<script type="application/javascript" src="./some-path/ecs.js"></script>
<script>
  // define a component
  class Position {
    constructor(x, y) {
      this.x = x
      this.y = y
    }
  }

  // register the component
  ecs.register(Position)

  // create an entity
  const entityId = ecs.getNextId()

  // add or update the entity data
  ecs.set(entityId, new Position(25, 25))
</script>

Module import

import { ecs } from 'ecsjs'

// define a component
class Position {
  constructor(x, y) {
    this.x = x
    this.y = y
  }
}

// register the component
ecs.register(Position)

// create an entity
const entityId = ecs.getNextId()

// add or update the entity data
ecs.set(entityId, new Position(25, 25))

API

The primary interface for managing entities and components is the EntityMap class. A global instance ecs is exported for convenience, but you can also create your own instances.

Full Api Reference Documentation

ecs.register(...ComponentClasses)

Registers one or more component classes. Components must be registered before they can be used with an entity.

  • Throws: ComponentTypeKeyMissing if a class name is missing (e.g., anonymous classes).
  • Throws: ComponentAlreadyRegistered if a class is already registered.
  • Example: ecs.register(Position, Velocity)

ecs.getNextId()

Generates a unique entity identifier (a simple number). It reclaims IDs from destroyed entities when possible.

  • Returns: number

ecs.set(entityId, ...componentInstances)

Adds or updates one or more component instances for a specific entity.

  • Example: ecs.set(entityId, new Position(10, 20))

ecs.getMap(ComponentClass)

Returns the underlying ComponentMap for a specific component class.

  • Returns: ComponentMap<T> | undefined

ecs.get(entityId, ...ComponentClasses)

Retrieves component data for an entity.

  • If one class is provided, returns the instance.
  • If multiple classes are provided, returns an array of instances.
  • If no classes are provided, returns all components for that entity.

ecs.firstKey(KeyComponent, ...RelatedComponents)

Returns the entity ID for the first entity that matches the KeyComponent. If related components are provided, returns [id, ...relatedData].

ecs.firstValue(KeyComponent, ...RelatedComponents)

Returns the component data for the first entity that matches the KeyComponent.

  • Example: const [player, pos] = ecs.firstValue(Player, Position) ?? []

ecs.firstEntry(KeyComponent, ...RelatedComponents)

Similar to firstValue, but also returns the entityId as the first element in the array.

  • Example: const [id, player, pos] = ecs.firstEntry(Player, Position) ?? []

ecs.firstEntity(KeyComponent)

Returns an array of all components for the first entity associated with the KeyComponent.

ecs.entityValues(KeyComponent)

Returns an array of component arrays (all data) for every entity associated with the KeyComponent.

ecs.has(entityId, ComponentClass)

Checks if an entity has a specific component.

  • Throws: ComponentNotRegistered if the component class is not registered.
  • Returns: boolean

ecs.hasAll(entityId, ...ComponentClasses)

Checks if an entity has all of the specified components.

  • Throws: ComponentNotRegistered if any of the component classes are not registered.

ecs.hasAny(entityId, ...ComponentClasses)

Checks if an entity has at least one of the specified components.

  • Throws: ComponentNotRegistered if any of the component classes are not registered.

ecs.remove(entityId, ...ComponentClasses)

Removes one or more components from an entity.

ecs.destroyEntity(...entityIds)

Removes all components from the specified entities and reclaims their IDs for future use.

ecs.clearComponents()

Removes all entity data and reclaims all IDs, but keeps the registered component classes.

ecs.clear()

Resets everything, including removing all registered component classes.

ecs.iterator(KeyComponent, ...RelatedComponents)

Returns an iterator for entities matching the KeyComponent.

  • Example:
    for (const [id, pos, vel] of ecs.iterator(Position, Velocity)) {
      // process
    }

ecs.query(KeyComponent, ...RelatedComponents)

Creates a reusable query for entities that possess at least the KeyComponent.

  • Returns: A ComponentQuery object that can be iterated or used to fetch specific results.

ecs.printTable(components?, properties?)

Prints component maps to the console in a tabular format for debugging.

ecs.printEntity(entityId, properties?)

Prints all component data for a specific entity to the console in a tabular format.

EntityMap.parse(json) (static)

Restores an EntityMap instance from a JSON string.

EntityMap.createWithTracing(funcFilter?) (static)

Creates a new EntityMap wrapped in a Proxy that logs all method calls to the console for debugging.

License

Licensed under GNU GPL v3

Copyright © 2013+ contributors