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

@jacekpietal/object-pool

v1.1.0

Published

zero-dependency Object Pool manager for instantiating, getting, reuising and destroying objects in javascript

Downloads

2

Readme

Installation

npm install @jacekpietal/object-pool --save

Usage

const ObjectPool = require("@jacekpietal/object-pool");
const pool = new ObjectPool(
  // you should provide this function
  function factory() {
    // that returns a new object instance
    // can be async
  }
);

// will return value from pool
// if pool empty uses factory function
const object = pool.get();

// returns value to the pool
pool.put(object);

// prints 1
console.log(pool.size);

// removes value from pool
pool.delete(object);

// prints 0
console.log(pool.size);

// pre-fills pool up to size
// useful I think only for async functions
pool.size = 100;

// empties pool
pool.size = 0;

// size manipulations do trigger as required
// `put` and `delete` events for each change

With Promises / async functions

If you want to use async functions be sure to put the same thing you get from the pool.

const pool = new ObjectPool(async () => "value");

async function test() {
  const once = pool.get();
  const value1 = await once;

  // put back the promise reference
  pool.put(once);

  const again = pool.get();
  const value2 = await again;

  // prints:
  // {
  //  once: Promise { 'value' },
  //  again: Promise { 'value' },
  //  value1: 'value',
  //  value2: 'value'
  // }
  console.log({ once, again, value1, value2 });
}

test();

Events

// all actions are accompanied by EventEmitter events

pool.events.on("get", (value) => {
  console.log({ get: value });
});

pool.events.on("put", (value) => {
  console.log({ put: value });
});

pool.events.on("delete", (value) => {
  console.log({ delete: value });
});

API

pool.get(); // returns get value from pool

pool.put(object); // puts put value to end of pool

pool.delete(object); // deletes object from pool manually

Tests

Please check tests for more complicated cases:

$ yarn test
yarn run v1.22.5
$ jest
 PASS  ./index.spec.js
  GIVEN ObjectPool instance
    ✓ THEN It should create (2 ms)
    WHEN pool.get is called
      ✓ THEN even if pool.size is 0 (1 ms)
      ✓ THEN It should return new instance of factory call
      ✓ THEN events.get should be emitted (1 ms)
    WHEN pool.put is called
      ✓ THEN pool.size should be greater than 0 afterwards
      ✓ THEN events.put should be emitted (1 ms)
    WHEN promise is `put` and then `get` again
      ✓ THEN it still works as expected (1502 ms)
    WHEN pool.size is set to 0 on non-empty pool
      ✓ THEN events.delete should be emitted for each item (1 ms)
    WHEN pool.size is set
      ✓ THEN pool size adjusts and is being filled accordingly (2 ms)
      ✓ THEN pool size adjusts and is being trimmed accordingly (1 ms)

Test Suites: 1 passed, 1 total
Tests:       10 passed, 10 total
Snapshots:   0 total
Time:        2.668 s
Ran all test suites.
Done in 3.41s.

License

MIT