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

lill

v0.5.0

Published

Lightweight linked list implementation with small memory footprint.

Downloads

30

Readme

LiLL - Light Linked List

Lightweight linked list implementation with a small memory footprint.

Build StatusCode Climate

There are few implementation of the linked-list structure in the JavaScript ecosystem, but most of them create a bunch of extra objects to store metadata about the list. We didn't like that, so we created LiLL.

The linked list data structure really shines in situations when you frequently need to iterate over a list of items, but only modify the list's structure infrequently. Linked lists are ordered simply by having each object in the list reference its previous and next siblings. Linked lists are much faster to iterate over than standard loops.

Built With Symbol

LiLL is using new ES6/ES2015 feature called Symbol. This primitive makes it possible to store the linked list's metadata on original objects being linked together. There are no collisions in property names and it doesn't interfere with your objects in any way. If you dislike mutating your objects in any way, this library isn't for you.

Installation

LiLL is available as ES5 compatible NPM module without any additional dependencies.

NPM

You will need to polyfill ES6 Symbol if using library in older environments like IE.

How to use LiLL

First you need container object, We call it owner. It can be anything that is recognized as object or function by typeof operator (except null of course). On top of that, owner has to pass the check by Object.isExtensible().

var Lill = require('lill');
var owner = {};
Lill.attach(owner) === owner;

LiLL creates only single state object that is stored on the owner using Symbol. Basically it means that LiLL's public API is completely stateless. One small disadvantage this approach is that you must pass an owner object to every LiLL method call.

Adding to the list

To keep the memory footprint low, information about neighbors are stored on actual items using Symbols. That means you can add only items capable of this. No primitive values allows that. Similarly to owner, you can use objects or functions and item has to pass the check for Object.isExtensible().

var item = { foo: 'bar' };
Lill.add(owner, item);

Removing from the list

This works very similar to adding. Previously added properties are completely removed from the item and neighbors are modified accordingly.

Lill.remove(owner, item);

Iterating the list

Currently only basic iteration is supported and looks like this.

var iterate = function (item, i) {
	// do your work with item
};

Lill.each(owner, iterate, optionalContext);

Be warned that you should not modify the list during iterator invocation as it may cause unexpected behavior. This comes from nature of linked list structure as any changes in the chain of items could break iteration.

Iterable interface

LiLL supports iterating over items using the Iterable interface. You can use higher order functions (eg. wu.js) to achieve much fine grained control while maintaining speed of iteration. However be warned that without native support for iterators there might be increased number of objects to be collected by garbage collector.

const things = wu(Lill.iterate(owner))
	.pluck('prop')
	.map(createThing);

for (const thing of things) {
	// do your work with the thing
}
const thingsArray = Array.from(things);

Finding item in the list

If you are looking for a particular item, using each means that it will iterate over every item. Using find quits the loop once the predicate function returns true. If no predicate is fulfilled, the null is returned.

	var predicate = function (item, i) {
		if (item.name == "correct") {
			return true;
		}
	};
	
	item = Lill.find(owner, predicate, optionalContext);
	item.name == "correct" // true

Number of items in list

There is internal counter of the items currently on the list. This can be used for example to randomly pick item from the list.

Lill.getSize(owner);

Accessing the items

Every item on the list keeps information about it's neighbors. You can access such informations like this.

Lill.getNext(owner, item);
Lill.getPrevious(owner, item);

You might want to know where the list begins too. This works very similar.

Lill.getHead(owner);
Lill.getTail(owner);

Now you could iterate the list like this.

var item = Lill.getHead(owner);
while (item) {
	// do your work with the item
	item = Lill.getNext(owner, item);
}

Clearing the list

To conveniently remove all items from the list, just call the following.

Lill.clear(owner);

Detach the list

If you want remove all items from the list and dispose of everything that LiLL was using, simply call detach.

Lill.detach(owner);

A detached object can be reattached later if necessary. If you use any of LiLL's operation methods on a detached object an error will be thrown.

Check for attached object

If you want to check if some object is used as owner by LiLL, simply call this.

Lill.isAttached(owner);

Known limitation

Due to simplicity of the solution, single object can be "owner" only once. However item can be present in multiple lists without influencing each other.

Tests

LiLL is fully tested. You can check out the result of the tests at Travis CI or clone repository for yourself, run npm install first and then npm test.