Standard JavaScript Containers Library
A collection of typed containers for JavaScript.
npm install --save sync-containers
This is my personal collection of common JavaScript containers, plain and simple. I was fed up with writing the same boilerplate code over and over again, and I needed a library over which I had some control. I ended up writing my own.
☝️ We could use a helping hand. If you think you're up for it, open an issue.
⚠️ These containers have not extensively been tested just yet. However, I do make use of them in my projects, so most common use cases should work. Above that, you are invited to make use of them and report any issue on GitHub.
The interfaces are fully documented. You can find the full documentation here.
Examples at the bottom of the README.
Interfaces
Container | Type | Unique | Order |
---|---|---|---|
Bag | T | No | No |
Set | T | Yes | No |
List | T | No | Yes |
Vector | T | No | Yes |
Queuelike | T | No | Yes |
Dict | Pair<K, V> | Yes | No |
MultiDict | Pair<K, V> | No | No |
Implementations
A ✔️ indicates that the implementation has been completed. On the other hand, a missing ✔️ means that implementation is stil pending.
Unordered Containers
Name | Memory | Add | Remove | Member | |
---|---|---|---|---|---|
✔️ | set/string | O(n) | O(1) | O(1) | O(1) |
✔️ | set/es6 | O(2n) | O(1) | O(1) | O(1) |
✔️ | dict/string | O(n) | O(1) | O(1) | O(1) |
✔️ | dict/es6 | O(2n) | O(1) | O(1) | O(1) |
✔️ | dict/multi/string | O(n) | O(1) | O(1) | O(1) |
✔️ | dict/multi/es6 | O(2n) | O(1) | O(1) | O(1) |
queue | O(n) | O(1) | O(1) | O(n) | |
✔️ | stack | O(n) | O(1) | O(1) | O(n) |
priority-queue | O(n) | O(log(n)) | O(log(n)) | O(n) |
Ordered Containers
Name | Memory | Insert | Append | Prepend | Member | At | Next | Prev | |
---|---|---|---|---|---|---|---|---|---|
✔️ | vector | O(n) | O(n) | O(n) | O(n) | O(n) | O(1) | O(1) | O(1) |
✔️ | list/single | O(n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(n) |
list/double | O(2n) | O(n) | O(1) | O(1) | O(n) | O(n) | O(1) | O(1) |
Queue-like structures
Name | Enqueue | Dequeue | Reschedule | |
---|---|---|---|---|
✔️ | stack | O(1) | O(1) | n.a. |
queue | O(1) | O(1) | n.a. | |
priority-queue | O(log(n)) | O(1) | O(log(n)) |
Consult the API docs for more information on how to use them.
Examples
Lists and vectors
difficulties.append'hard')difficulties.prepend'easy'difficulties.insertBeforedifficulties.end, 'medium') // takes long for SL-lists for of difficulties console.loglevel // Output:// easy// medium// hard difficulties.clear// container now empty
The same example can be used with an ArrayVector
, the only difference being
that operations have a different time complexity (see the tables above).
Sets
, fooService = new FooService services.addtestServiceservices.hastestService // trueservices.hasfooService // falseservices.addfooServiceservices.addtestService // throws error: already added // order of iteration is NOT guaranteed!for of services console.logservice.name set.removetestService
Queues and priority queues
Building on top of the first example:
levels.enqueuenew HardLevellevels.enqueuenew EasyLevellevels.enqueuenew MasterLevel levels.dequeue // easy levellevels.dequeue // hard levellevels.dequeue // master level
The same example can be used with a normal queue, the only difference being
that levels will be dequeue()
-ed in the order they were inserted.
Dictionaries
// a named set is a special kind of dict users.addPair"bertie", users.add users.hasKey"benjamin" // trueusers.hasKey"anne" // false users.getValue"benjamin" // { email: ... }users.getValue"mary" // throws error for of users console.log`User has email `
Credits
Thanks to Wolfang de Meuter of the Software Langagues Lab for teaching me the data structures which are contained in this library and for providing a reference implementation which highly influenced this library.
Support
You might also be interested in knowing how this library's iterators work.
Need to go asynchronous? Check out our asynchronous containers library.
Found an issue? A certain mistake? Need a certain kind of container? File an issue or send me a pull request.