22

I'm loving node JS and, coming from a Java background, am interested in even trying it out for some projects where node may seem a bit of a stretch, such as a search engine project.

One thing I've been a bit confused by is it seems JavaScript is lacking traditional data structures, for example a set, which has a precise definition extending even beyond computer science as it has been used in mathematics before computers existed (basically a list that doesn't allow duplicates). It seems when using node JS there is no library like Java.util that has these basic data types that I have grown accustomed to, I realize I could create them myself but this just adds more overhead to the project.

Are there any libs for node (or JavaScript in general) that address this? I think node has a lot of potential to replace the use of a language like Java for a lot of projects as it has so many advantages in terms of development speed, but having to recreate data structures that are taken for granted in a more mature platform could be too much overhead for a small project.

I apologize if there are other questions like this, however I spent some time searching and didn't come up with much.

Rick
  • 16,612
  • 34
  • 110
  • 163
  • 3
    There is `Set` in harmony (run `node --harmony`). – pimvdb Aug 30 '12 at 19:59
  • thanks, I'm not sure what `harmony` is, do you have a link as my google searches on this don't seem to be turning up link to the project – Rick Aug 30 '12 at 20:03
  • 2
    It's basically the name of a work-in-progress of the next ECMAScript version. For sets, see [this](http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets). – pimvdb Aug 30 '12 at 20:05

8 Answers8

14

es6 has a Set class built in:

new Set([iterable]);

see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Adrian Macneil
  • 13,017
  • 5
  • 57
  • 70
  • 2
    Note: node.js now implements this "out of the box." If you're just looking for Set and Map like me, this is all you need, and rock on. – lance.dolan Mar 15 '17 at 20:54
  • 1
    Is this essentially a hashset? I would like O(1) for contains/has functions. – CamHart Jan 21 '18 at 08:27
8

Collections.js has Lists, Maps, Queues, Sets, and Heaps, all with consistent interfaces. Github.

weiyin
  • 6,819
  • 4
  • 47
  • 58
6

it seems JavaScript is lacking traditional data structures...

Yes, this is javascript, the very concept and implementation of data structure is done quite differently from languages like Java.

I'm not sure that you're really going to find what you're looking for with Javascript. Howver, there are some libraries like underscore that should make it easier to build the type of structures that you want.

Gates VP
  • 44,957
  • 11
  • 105
  • 108
6

Its no longer true that node.js doesn't have Set and Map objects among other things. node.js has had them since at latest v12.

But of course, if you want libraries like java has, check npm or github. You're not limited to what comes standard in node.js.

Community
  • 1
  • 1
B T
  • 57,525
  • 34
  • 189
  • 207
  • you're not limited neither in java, but you have at least some coherency in the language kernel – Gab Sep 05 '16 at 12:01
  • @Gab Leave your language-war-bait at the door. Your comment has nothing to do with my answer or the OP's question. – B T Sep 05 '16 at 23:48
5

Have you looked into Underscore.js? http://underscorejs.org/

It's not a one to one with java.util but it provides a bunch of commonly needed utility functions.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73
5

As a lighter and faster alternative to Underscore.js, Lo-Dash (http://lodash.com/) is getting traction those days... But this is not Java.util! :-)

1

Have a look at this one: https://github.com/chenglou/data-structures

I think it fits what you are looking for.

Mathieu M-Gosselin
  • 1,225
  • 1
  • 13
  • 17
0

js-sdsl

A javascript standard data structure library which benchmark against C++ STL.

This library has strict time complexity guarantee and can be used with confidence.

The latest beta version includes iterator functions which can be used like iterator in c++.

Included data structures

  • Vector
  • Stack
  • Queue
  • LinkList
  • Deque
  • PriorityQueue
  • Set (using RBTree)
  • Map (using RBTree)
  • HashSet (for reference only)
  • HashMap (for reference only)

Usage

To help you have a better use, we provide this API document.

Zilong Yao
  • 54
  • 4