4 ms·
I'd like a decent standard library. These days I work mainly with C#, and to a lesser extent Typescript and JavaScript - the anemic nature of the ECMAScript st
by GordonS 8y ago
I'd like a decent standard library.
These days I work mainly with C#, and to a lesser extent Typescript and JavaScript - the anemic nature of the ECMAScript standard library is really jarring.
- dehrmann 8y agoI mostly work with Java and Python. Whenever I use Javascript, the lack of basic data structures other that arrays and objects is always jarring.
- city41 8y agoThere is also Map and Set.
- ilikehurdles 8y agoComing from clojure, I find that it’s not data structures that JS lacks, just functions/methods to manipulate them and a consistent approach to their mutability and return values.
- hajile 8y agoObjects, Arrays/lists (technically objects), TypedArrays, Set, Map (with weak variants). What other important structures did you have in mind?
- dehrmann 8y agoArrayDeque, LinkedHashMap, TreeMap, PriorityQueue, even LinkedList. Their interfaces have gotten pretty built-out, too, with methods like computeIfAbsent. It also has a rich collection of concurrent data structures, but those aren't useful in Javascript (yet).
- hajile 8y agoLucky for you, JS already has good solutions for most of those things. LinkedList is almost indistinguishable from an growable array. Since the JS array grows by default, you're covered for the kinds of things you'd want a linked list for (in fact, most older JS implementations actually implemented arrays as linked lists). ArrayDeque's big claim to fame is add/remove from both ends. That's also covered by the standard array (shift/unshift, pop/push). Linked hashmap's big feature is maintaining insertion order. The JS Map type does this automatically. JS Objects don't do this officially, but since so much legacy code depends on this "feature", it is implemented for objects across all major browsers. Priority Queue isn't in JS, but I'd argue it's rather niche and rather few async frontend JS projects need it. On the backend, one extra import is hardly a big deal. There is also no treemap. They are simple to implement (esp with proxies). JS objects store all keys as strings and `Object.keys(foo).sort()` is probably about as fast. As an additional issue, JS maps can have keys of different types, so deciding how to perform that sort would be very difficult. This also seems like a niche issue that belongs outside the standard library.
- int_19h 8y ago> LinkedList is almost indistinguishable from an growable array. Since the JS array grows by default, you're covered for the kinds of things you'd want a linked list for (in fact, most older JS implementations actually implemented arrays as linked lists). Not only they have different performance for various operations (append, prepend, indexing, removal in the middle), but perhaps even more importantly, linked lists can share data if it's immutable. So an array is not a replacement.
- hajile 8y agoYou'd be correct -- if we were talking about a lower-level language. JS implementations are free to choose whichever array representation works best be that hashtable, struct, class, linked list, array, or whatever. Modern JITs use a couple different approaches that have been found to be fastest and can even switch representations on the fly based on several factors (is the array homogeneous, is it sparse, is it short, med, or long, and so on). Even moving away from JS, there should be a healthy skepticism for basic O notation about data structures and algorithms. When your processor is thousands or millions of times faster than your ability to send over data from memory, the notion of what is faster changes a lot. If my linked list misses in cache because it's memory isn't sequential, then the time it takes to add an element and read may be much longer than copying an array and expanding it a few entries (especially if a fill pointer is used).
- smacktoward 8y agoThis! So much this. JavaScript-the-language is in decent shape now, but JavaScript-the-ecosystem is a complete and utter mess. Having even a small standard library would go a long way towards cleaning that up.
- laurentl 8y agoYes. TFA mentions it in passing, but to me JS’s main issue is not the language but the ecosystem. You can’t install an npm package without inheriting hundreds of dependencies. Reading a typical package-lock.json file makes me cringe. I feel that a fully-fledged standard library (python’s comes to mind) would help a lot in that regard.
- qudat 8y agoJS cannot simply release a standard library and every browser will autoupdate to have it. It takes browsers years to get up to date with the latest in ecmascript. So any solution would look at 5 years before adoption. This is why transpilers have taken over: polyfill and shim all the things. It will still add bloat to js bundles, it will still require npm packages. This is the world we live in and it is unique to javascript.
- underwater 8y agoThe best time to add a decent standard library was 10 years ago. The second best is today. Shipping a decent set of core libraries would do wonders for package sizes. You could cover a lot of use cases with a relatively small API surface area (the classic 80/20 rule) and you'd find that a lot of duplication in implementing the fundamentals in third party packages would just go away. Browsers update pretty quickly now, anyway. It's likely that in the future we will see most sites using a lightweight package for modern browsers and a heavy polyfill for IE, which is fine by me.
- int_19h 8y agoAnd yet ES6 exists. If the community wanted, it could do the same with the standard library.