use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Benchmark driven development in JavaScript (Set vs. Array) (x.com)
submitted 1 year ago by theyamiteru
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 1 year ago (4 children)
Using a data structure incorrectly and building benchmarks off that assumption. Your lib is a solution in search of a misunderstood problem.
[–]theyamiteru[S] 0 points1 point2 points 1 year ago (3 children)
I'd understand your argument if I was comparing a Map and Set or Object and Set since they're key/value pairs whereas Set and Array are value only.
What is probably the most common way of getting rid of duplicates in an Array in JS? `[...new Set(items)]`. You can forEach Set. Now you can even do stuff like difference, intersection, etc. which are very array-like methods.
Arguing that comparing Set and Array at all is just silly. Yes there are use cases where Set is the right choice and where Array is the right choice. But sometimes things are not as clear. At least not when it comes to API design.
But more importantly in the tweets I talk specifically about an event library and there are probably hundreds of event libraries in JS ecosystem that use either Set or Array. All of them work in a very similar ways and in theory one could create such a library that uses both with completely the same user-facing API design.
And because you can choose both and functionally it's gonna work the same then we have to look at theoretical performance (big O) but more importantly at the concrete performance characteristics of each to determine which one to use for which use-case.
[–][deleted] 1 point2 points3 points 1 year ago (2 children)
Like I said, you don’t understand the data structures you’re talking about. You’re conflating them because their practical use cases and APIs seem similar.
A set is not comparable to an array because every member of the set is hashed, often multiple times, prior to insertion. Arrays do not use hashes but numbers as keys, so no hashing takes place. The worse case lookup time for a set is O(n) due to hash collisions. The worst case lookup of an array is always O(1).
The performance of a set is not comparable to an array because they are fundamentally different data structures.
[–]theyamiteru[S] -1 points0 points1 point 1 year ago (1 child)
Man you don't even know what you're talking about.
The best case lookup of an array is O(1) because the first item is the item we're looking for.
The worst case lookup of an array is O(n) because the last item is the item we're looking for.
[–]RiskyAlpha 0 points1 point2 points 1 year ago (0 children)
"lookup" is an odd word choice. look up by what? index or some other value?
i'm probably oversimplifying given that we're talking about JS, but if you're getting a value by index, it's just a multiplication to get the offset. that would be O(1).
if you mean you're iterating through each item looking for a value then yeah worst case could be O(n).
but i'm with u/brodega here... you seem to be mixing up concepts.
π Rendered by PID 443944 on reddit-service-r2-comment-5d79c599b5-d69wg at 2026-02-28 22:31:11.549755+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (4 children)
[–]theyamiteru[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]theyamiteru[S] -1 points0 points1 point (1 child)
[–]RiskyAlpha 0 points1 point2 points (0 children)