Which is the best way to use less RAM and better?

Hello, I'm making a condition based command which is going to check ifs. But I don't want to code a shit code which is leaking memory. So, I coded something like:
if (message.content.toLowerCase().includes("some_text"))
return message.react("some_awesome_emoji");
if (message.content.toLowerCase().includes("some_dumb_word"))
return message.react("some_dumb_emoji");
// and so on
if (message.content.toLowerCase().includes("some_text"))
return message.react("some_awesome_emoji");
if (message.content.toLowerCase().includes("some_dumb_word"))
return message.react("some_dumb_emoji");
// and so on
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
Use && and || in your code
Neo
NeoOP3y ago
So instead making many ifs, going for OR is a better choice? But you know, in there; the output is also different.
조아오
조아오3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
조아오
조아오3y ago
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
Azrael⛧⸸
Azrael⛧⸸3y ago
you can use switch statements as well
switch(message.content.toLowerCase().includes) {
case 'some_text':
return message.react("emoji");
case 'some_dumb_word':
return message.react("another_emoji");
}
switch(message.content.toLowerCase().includes) {
case 'some_text':
return message.react("emoji");
case 'some_dumb_word':
return message.react("another_emoji");
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
Switch vs if statements are a huge difference in performance and less spaghetti code idk what you're talking about
Azrael⛧⸸
Azrael⛧⸸3y ago
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 method
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
Azrael⛧⸸
Azrael⛧⸸3y ago
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
조아오
조아오3y ago
It would be noticeable if you were dealing with hundreds of thousands of requests I would bet microseconds
Azrael⛧⸸
Azrael⛧⸸3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
It is
Azrael⛧⸸
Azrael⛧⸸3y ago
조아오
조아오3y ago
If they are really if focused in speed, their first mistake is choosing js
Azrael⛧⸸
Azrael⛧⸸3y ago
Fair enough lmao Yes agreed, that was my point, im just trynna help him to learn good practice
Azrael⛧⸸
Azrael⛧⸸3y ago
?
조아오
조아오3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Azrael⛧⸸
Azrael⛧⸸3y ago
Fair enough, but i use typescript, so that won't be possible in a way
조아오
조아오3y ago
why not? valorant[interaction.options.getSubcommand() as YourPossibleThings](interaction)
Azrael⛧⸸
Azrael⛧⸸3y ago
Idk, i haveto do something like
const subcmd = interaction.options.getSubcomand();

valorant[subcmd as keyof typeof <something>](interaction)
const subcmd = interaction.options.getSubcomand();

valorant[subcmd as keyof typeof <something>](interaction)
getSubcommand() returns something that is with <Omit> and i dont have nerves for that wait nevermind
조아오
조아오3y ago
getSubcommand() returns a string if I'm not dumb
Azrael⛧⸸
Azrael⛧⸸3y ago
Im braindead it returns string, you're right Well i will use that, thank you
조아오
조아오3y ago
You can even use a map tbh valorant.get(interaction.options.getSubcomand())(interaction)
Azrael⛧⸸
Azrael⛧⸸3y ago
my methods are not mapped
조아오
조아오3y ago
That depends on you
Azrael⛧⸸
Azrael⛧⸸3y ago
anyways, this is going off topic lmao, i hope the OP finds a way to make better code eventaully
조아오
조아오3y ago
OP, this is a good way to do it
Neo
NeoOP3y ago
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
Inky
Inky3y ago
Switch shines more when you want to do different operations based on the possible values of an expression (e.g. enum values)
Neo
NeoOP3y ago
Yeah inline looks like shit 💩 if better.

Did you find this page helpful?