Vanja's Blog

Subscribe to Blog via Email

Enter your email address to subscribe and receive notifications of new posts by email.


Recent Posts

Top Posts & Pages




Collections Library for JavaScript

How to use Collections.js library in JavaScript app

Vanja PetreskiVanja Petreski

While working on the JavaScript project I had a need for a data structure that supports:

After some research and testing I opted out for Collections.js library.


Collections.js as described by authors:

“This package provides JavaScript implementations of common collections, with many useful features.”

— Montage Studio

You have to install Node and NPM first. Then test your installation:

$ node -v
$ npm -v

In the working directory execute this command to fetch Collections.js library:

$ npm install collections

You will notice that node_modules folder was created with the library installed.

Since I am not using Node.js in my project, I can’t use this:

var SortedArrayMap = require("collections/sorted-array-map");

Browsers don’t have the require method defined like Node.js, so I needed a way to write code that uses require in the same way that I would use it in Node. Welcome to Browserify!


“Browserify lets you require(‘modules’) in the browser by bundling up all of your dependencies.”

— James Halliday

So basically, you can write require(‘modules’) code and then use Browserify to bundle everything into one JavaScript file that can be used in a browser without the Node.js.

Let’s see how to use it for Collection.js. Execute this command to install Browserify:

npm install -g browserify

Now in the working directory where you installed Collections.js navigate to node_modules/collections folder and execute this command:

$ browserify collections.js -o bundle.js

File collections.js uses require and above command created bundle.js without requires, so it can now be used in browsers without Node.


I wanted to compress/minify generated JavaScript file so I used UglifyJS – JavaScript compressor/minifier written in JavaScript.

Let’s install it first:

$ npm install -g uglify-js

And now use Browserify and Uglify combo:

browserify collections.js | uglifyjs > collections.min.js

Now you can include generated collections.min.js into your app and start using it.


Simple self-explaining example of using the library:

var data = new SortedArrayMap();

data.set(4, 'four');
data.set(1, 'one');
data.set(5, 'five');
data.set(3, 'three');



var vals = data.sorted(function(left, right) {
return left-right;



Don’t reinvent the wheel – use libraries! 🙂

Software Engineer & Digital Nomad

Comments 0
There are currently no comments.