Which is the best way to use less RAM and better?
Hello, I'm making a condition based command which is going to check
if
s. But I don't want to code a shit code which is leaking memory. So, I coded something like:
I feel like this will be leaking memory.
If you have a suggestion...
Please let me know if there's any other way to lessening code. I'll be using it later on my new codes.39 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Use
&&
and ||
in your codeSo instead making many ifs, going for
OR
is a better choice?
But you know, in there; the output is also different.Although it can be optimized
This type of code won't cause memory leaks
And the performance gain is minimal, unless your bot has tons and tons of data to process
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Memory leaks is when the garbage collector doesn't get rid of dumb code because is still being referenced somewhere
Nothing smt like this will cause
Exaclty
Here, you can read this https://blog.logrocket.com/escape-memory-leaks-javascript/#what-memory-leaks to learn more about memory leaks and how they work
you can use switch statements as well
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Switch vs if statements are a huge difference in performance and less spaghetti code idk what you're talking about
if and else statements go synchronously like, javascript checks each if and else statement, as in.
It can work, because it passes through
.includes
which is an object methodUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
when it comes to large checks yes
Also I am wrong for this my bad
I never said it will decrease ram usage, the code will run faster, easier to read and lessens the code
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View

Which has less code and easier to read?
if and else goes step by step, while switch does not
It's very situational
Okay if you think so
It would be noticeable if you were dealing with hundreds of thousands of requests
I would bet microseconds
We dont know what will the bot creator do in the future so im helping him with lessening and optimizing code
if he has a lot of subcommands, switch statement will be really useful
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
It is

If they are really if focused in speed, their first mistake is choosing js
Fair enough lmao
Yes agreed, that was my point, im just trynna help him to learn good practice
?

Also "a lot", the max amount of commands per bot is 200 idk the global limits but supposing counting with subcommands it would be near 1k which is a ridiculous small dataset
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Fair enough, but i use typescript, so that won't be possible in a way
why not?
valorant[interaction.options.getSubcommand() as YourPossibleThings](interaction)
Idk, i haveto do something like
getSubcommand() returns something that is with <Omit> and i dont have nerves for that
wait nevermind
getSubcommand() returns a string if I'm not dumb
Im braindead it returns string, you're right
Well i will use that, thank you
You can even use a map tbh
valorant.get(interaction.options.getSubcomand())(interaction)
my methods are not mapped
That depends on you
anyways, this is going off topic lmao, i hope the OP finds a way to make better code eventaully
OP, this is a good way to do it
Sorry for not knowing c++ 😂
@tipaka, I mean with rust;A bot with 2,000 guilds can use 2MB RAM but discord.js uses 30MB+. Well, node uses 20MB~ already 😂
And yeah, my point was about writing less code but the code still doing same work as bad code.
Not really
So switch is more handy for this job than a lot ifs?
Just a simple thing, adding reaction to message.
Okay then switch better
I mean
It also looks clean too
Switch shines more when you want to do different operations based on the possible values of an expression (e.g. enum values)
Yeah inline looks like shit 💩
if better.