- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
where is my favourite bsearch with properly written comparators
Admin
My guess is, someone with less experience in JS than in another language figured, "I need some way to reference objects by a string key, and I don't trust javascript objects. Can't even rely on an object having a property..."
Admin
You never know when JS/TS maps might break. Now, the Object Store... Now that's solid!
Admin
Seeing there isn't a delete, this looks suspiciously like Array.
Admin
ObjectStore is created with a free "toString" item!
Admin
A set of objects stored with sequentially-numbered indices? What an amazing invention! We should give it a new name... "array" has a nice ring to it.
Admin
Well... this is just a rant because the submitter doesn't understand why the object exists. Imagine the use case is to usually push objects, but you also want to change some later and you need some kind of ID, because the objects themselves do not provide such a thing. One idea would be to use store[autoid()]=obj, instead of store.push(obj), but the second one has a per-store unique id, where autoid() can only be global. Therefore store.push(obj) is a lot better than store[autoid(store)]=obj, and with the second you'd have to store the id in the map itself, like: autoid(store) { return ++store["counter"]; }, which would pollute the map, so whenever you use Object.keys(store) you'll have to remove "counter" to get only the actual IDs. If you now make functions for all that you are really close to the original object.
But the real WTF is the implementation of _getNextUniqueKey which recursively increments the counter, every time checking whether the array of keys does not contain the current value. Why is nobody talking about that?
Admin
How about the fact that _getNextUniqueKey is recursive? Because it isn't WTFy enough unless you accidentally blow up the call stack?
Admin
Does this storage work like other storage offers: very low price for the first month (to get you in the door) and then an astronomical sum thereafter?
Admin
It will probably be fine. At least reliable enough to pass testing.
Now, if _counter has somehow become a float and there are enough objects being stored that _counter++ === _counter, that might become a problem...
But that should only happen in production.
Admin
I wonder what would happen to the program if you swapped out auto incremented keys with content hashing.
Admin
The programmer is saying they want "a raise".
Admin
I thought most engines treated that as a proper tail call, and avoided a ballooning call stack?
Not defending it though. If that's the case it's likely dumb luck instead of actual knowledge.
Admin
I almost wonder whether this is for obfuscation?
Admin
If _counter is already used as a key when calling _getNextUniqueKey, it get's incremented twice! Better be sure :)
Also, JS Objects and prototypal inheritance are a pretty good fit for their use case. JS Arrays are just a special kind of "object"/map, as others pointed out, pretty much like a working version of this abomination.
Admin
Also,
_getNextUniqueKey
isO(n)
instead ofO(1)
-Object.keys(...).contains
should instead becurrentIndex in store
. So many wonders, so little space!Admin
Methinks the genius behind this was a big time fan of the ol' Java way of doing things, where implementing an "ObjectStorage" like this would not only be encouraged, but necessary.
Admin
No, it is actually not incremented twice. Thought so too, but since
return (this._counter++)
is first returning the current _counter value and then incrementing, this works actually out. Double increments could be "achieved" by writingreturn (++this._counter)
instead ;-)Admin
You know what?! That's actually clever... I mean, still crappy, but clever crappy. Just take a moment to think it through... I call push(a), it writes a to 0 and increments the counter to 1. I call push (b), it writes b to 1 and increments the counter to 2. Now I call set(2, c), it writes c to 2 and the counter stays at 2. When I then call push(d), it realizes the key 2 is already in use, increments to 3, calls itself again, successfully writes d to 3 and increments the counter to 4.
This means... they KNEW it was shitty having push AND set writing to the array, and made it "bullet proof" for the case that someone calls set in between of all the push calls.... :-D
Admin
I haven't dug too deep into _getNextUniqueKey so I may be off base here. But I wonder if originally there was a delete(key) method, or at least the perceived requirement to be able to support a delete(key) method?
In the presence of deletions, the _getNextUniqueKey as written doesn't deliver [highest key value]+1 but rather [first unused key value]. Which might be based on somebody's cockamamie belief that key reuse somehow saves either storage space or key values. At the obvious expense of returning the wrong (or at least unexpected) value if any other code held onto a key across a deletion made elsewhere.
All in all, this is really a great foot-gun.
Admin
Also, no attempt to deal with the weird key values such as
"constructor"
.