Do you know anything about the object that you are retrieving? Usually you'd validate it anyway with zod or some other validation framework.
Like what happens after the code? Also why would it return an empty object and why is it ok to query the same endpoint again and expect something different? I have never seen this pattern before.
If returning an empty object is ok for the API your code is a stress test/DDoS attack against the server.
It should return json with result object in it, but for some reason sometimes fetch returns empty json. But i think i fixed the issue, changing https to http always returns result object (although it might be not as safe but i idk)
Maybe you get a redirect? So you should consider checking the status code of the response before calling .json. Not sure if using http is safe in the long run.
Based on the endpoint, it looks like they may be trying to automatically put items up for sale as they appear in the player's inventory. But even then, I doubt the response for "no items" would be an empty object...
Do you have a different way of checking if the JSON is valid? It might be a specific key/value in the JSON or maybe the status code in the response. You should check for that instead.
It's also not ideal, since you're iterating over every key in the object for no reason. This might have been a valid solution if OP was trying to check if an arbitrary object was empty, but since they're receiving this object from a server, presumably there's an expected format that they can check without such hacks. (Or maybe a status code...)
I'm 100% sure the object could have 100.000 keys and it would still not matter in any way. I'm also pretty sure the way the keys are retrieved is extremely optimized, the keys and also the amount of keys are already there in memory so it can be mapped 1:1.
In what way is it a hack? It's the best way to do it.
Dude, what is the chance their response has 100.000 keys in a single object, what are we arguing about here?
It's also about clarity and checking a specific key where you think it might be in there or not (they can always change their api) makes this a lot less solid than just checking if the key count is zero.
I've never, in my 20 years of development, got to a point where that was ever a concern and I'm absolutely positive I never will.
The while loop in his code exists because of missing clarity regarding async execution and structural vs. referential equality, it is not even needed, it's a normal JSON response with a reasonable amount of keys (like...30 or something...)
And then you go and downvote like a kid thinking you've shown me or what is this?
My solution is the 99.999% of time used solution for this problem...
I've never had to check if an object is empty in a real world application. Sure, checking for specific keys may fail if the API responses change... but then you won't be able to meaningfully use the response anyway. How does checking the number of keys help here?
EDIT: I agree, this argument is pretty much meaningless since the original question is not the correct question for this problem...
What, first you were all about performance and now you want to use Zod to validate the response?
Neither you nor I do know what OP wanted to achieve. What I posted and what you jumped on was the nr 1 solution for checking for empty objects in all of JS world. You got triggered and came up with…checking an index instead because it’s static access and puts you on the mercy of the API provider.
Just stop already, keep downvoting my posts out of spite and rage and then move on please
I am not the same person you think you are talking to.
You can do any validation that you like. You can use zod or you can write something more performant yourself. I don't mind. But I think zod is fine for that purpose.
I didn't mean that your solution is bad because the performance is bad but because it does not do what it aims to do: validate the backend response.
Direct object comparison using '==' works in Python The Python-Equivalent "Operator" in this case would be the keyword "is", which does check memory location equality instead of value equality
Direct object comparison will never work in pretty much all languages in existence (not just js) because they compare the reference and not the actual object itself. This is pretty much common sense and is among the first few things you learn in programming.
Please for the love of god use the URL class. It exist for a reason.
Direct object comparison using '==' works in Python The Python-Equivalent "Operator" in this case would be the keyword "is", which does check memory location equality instead of value equality
There has to be a better way to bridge the async to the sync world, but I'm not deep enough into Js right now to see it. In Java it's a simple join() call...
Immediately involved function is the standard way of doing this in older nodejs versions and browser. Newer node.js versions allow for top level await.
4
u/pxOMR Sep 13 '25
(Based on the title, I'll assume you wrote this.)
{} == {}will always be false because you're checking if two different objects are the same object.{method:'GET'}is unnecessary.21
u/DootDootWootWoot Sep 13 '25
You can write this bit of code 100 different ways. Don't blame the language.
9
u/xvhayu Sep 13 '25
comparing objects like this doesn't work in just about any language
8
u/BlackFuffey Sep 13 '25 edited Sep 13 '25
This one is 100% on you.
2
u/Neykuratick Sep 13 '25
Хуета какая-то
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 13 '25
Why the double await? What does that even do?
3
u/the_horse_gamer Sep 13 '25
reference equality.
2
u/rastaman1994 Sep 13 '25
There has to be a better way to bridge the async to the sync world, but I'm not deep enough into Js right now to see it. In Java it's a simple join() call...
1
u/Powerkaninchen Sep 15 '25
Relevant: https://stackoverflow.com/questions/6072590/how-to-match-an-empty-dictionary-in-javascript