I have the following code that what it does is obtain the data of a charge from a JSON and go through it.
I have two ways to do it and I don't know which could be the best or most efficient:
JSON file
{
"cargos":{
"DEVS":[
["Dev","RED"],
[ "Dev j","GREEN"]
]
}
}
First form
//la variable nameCargo = "DEV"
const cargos= ROLES.cargos[nameCargo]
installCargo(cargos)
function installCargo
installCargo(cargos) {
for(let [cargo,color] of cargos){
console.log('el Cargo= ',cargo,'el color=',color)
//aqui hay mas codigo pero es referente a Discord js
}
}
second form
const roles = ROLES.cargos[nameCargo].map(cargo=>installCargo(cargo))
function installCargo
installCargo(cargo) {
console.log(`Se esta instalando el cargo ${cargo}`)
//codigo de Discord js
}
I don't know which of the 2 ways would be optimal, the first one receives the entire array of charges and goes through it to be "installed" and the second one receives each one of the charges and "installs" it
Although the question seems opinionated, it is important to note that there are different types of optimizations for each function used in JS.
Computers today are fast enough that this type of optimization is necessary only when it is necessary to squeeze every millisecond of execution, which is why you would turn to a language like C, C++, Rust etc.
However, you can use the statement
console.time("etiqueta");
andconsole.timeEnd("etiqueta");
to measure the time it takes to execute a piece of code.this execution resulted in
so in this particular case the forEach() function is better. but I would recommend that you do it in your code and make a decision based on it.