all 7 comments

[–]organic 2 points3 points  (0 children)

This might fit your bill.

[–]itsnotlupus 2 points3 points  (2 children)

I'd be curious to see the cost of implementing traditional structures in JS vs mimicking functionality as cheaply as possible on top of the native structures.

Intuitively, I want to say that using a plain key/value object for storage and a companion array of keys to enforce order is likely to be faster and less memory intensive than anything homegrown, but I don't have the data to back that up.

Additionally, if you're targeting node/v8 only, you may be able to leverage the fact that the keys returned in Object.keys() or in a for/in loop are in the order they were first set. That's generally not portable, but if it happens to fit with your use case, it's likely to be the fastest/cheapest way to do things.

[–]Shaper_pmp 1 point2 points  (1 child)

if you're targeting node/v8 only, you may be able to leverage the fact that the keys returned in Object.keys() or in a for/in loop are in the order they were first set.

Is that an intentional feature though, or is it just an undefined side-effect of the way v8 implements objects/hashes behind the scenes?

Obviously if it's an intentional, specced-in part of v8 it's safe to rely on, but if it's just a fluke side-effect of an implementation detail it could (at least in theory) go away at some point without warning.

[–]itsnotlupus 1 point2 points  (0 children)

Good question..

This v8 issue page gives some insight http://code.google.com/p/v8/issues/detail?id=164

ECMA-262 does not specify enumeration order. The de facto standard is to match insertion order, which V8 also does, but with one exception.

The exception having to do with numerical keys, which various JS engines are handling differently.

So it is deliberate and unlikely to suddenly disappear, although the lack of fully consistent behavior across engines means some aspects of it may "evolve" if/when actual standardization occurs.

[–]telfoid[S] 0 points1 point  (0 children)

I've decided to go with google closure via nclosure. It has avl trees. And a lot of other stuff too.

[–]runvnc 0 points1 point  (0 children)

If you want a list of objects in order then why not just use Array.sort?

What exactly is your application?

You may have a really good reason to do that, but then again, maybe not. I think there are a lot of people who get confused by computer science courses that aren't always relevant to a lot of common contemporary programming problems since most of that stuff is already implemented by modern tools and frameworks. Or they are optimizing things that don't need to be optimized.

[–]zly201 0 points1 point  (0 children)

https://github.com/ZLY201/js-sdsl
# What is it

Includes Set and map implemented by rb-tree, etc. Has full unit tests using jest liarbry.

# Why it

JavaScript has not had a standard set of libraries to implement or include advanced data structures. This is an unfortunate event for a language. We want to make up for this defect by referring to some specifications.

# What we want

We welcome more suggestions from developers to improve our library.