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

reol

v0.2.2

Published

Keep an array of objects indexed by field, for super-fast retrieval.

Downloads

52

Readme

Reol.js

Keep an array of objects indexed by field, for super-fast retrieval.

Have you ever created an array, filled it with objects, and then wanted to sometimes retrieve the objects based on "id", but sometimes based on "slug"? Did you simple create a find(key, value) method? That's okay, I've done it too. Just keep reading though, you'll love this module.

Reol lets you create an array of objects pretty much like you've always done, but you can also specify fields to be indexed. The advantage of indexes is that retrieving an object based on the value of an indexed field is a lot faster.

How fast?

About twice as fast for tiny arrays (10 elements) and infintely faster for large (10000 elements) arrays.

See this jsperf test for exact data and to verify in your own environment.

Installation

Node npm install reol --save

Bower bower install reol

Others may simply copy index.js and put it where you like it. Reol should work in all js environments, including commonJS (node) and AMD (requireJS) as well as old-fashioned <script src="dist/reol.min.js"></script>-style.

Compatibility note: Reol depends on a global JSON object. Use a polyfill if you wish to support older browsers (<IE8).

Example

// Supports deep indexes
var list = new Heap({
  test: true,
  'parent.child': true
});

// Chain adds if you feel like it
list.add({
  test: 'meow',
  parent: {
    child: 'baahh'
  }
});

// Indexing works even with undefined and complex values, though the latter is not recommended
list.add({
  test: function(){
    console.log("seriously try to not do this");
  }
});

// Un-indexed fields also work
list.add({
  woot: 'mooo'
});

// Find all matches
list.find({ test: 'meow' });

// findOne will speed up best-case scenarios for queries on un-indexed fields
list.findOne({ woot: 'mooo' });

Caveats

Current version does not support non-unique indexes or queries on multiple fields. This will be added, see Todo.

Todo

Ordered by likeliness and simpleness to implement.

  • .get() based on array index (insert-order)
  • non-unique indexing (with option to enable)
  • Multi-field queries
  • Compound index
  • Sparse index
  • .remove() stuff
  • option to not auto-serialize objects but use (possibly custom) toString instead
  • A way too hook in external functionality, for example persisting data in localStorage

Contribution

I just threw this together in a few hours to solve my problem at hand. If you want to pick something on the todo list or have an opinion on how some method should be implemented (or maybe not implemented at all), please don't hestitate to create an issue, submit a pull request, tweet/email me or whatever.

For example I'm at the moment not sure if I should keep Raol as a very small and basic utility or extend it with rather complex (but some might argue essential) functionality such as .remove() and advanced indexing.

License

MIT