I have the following snippets in JavaScript:
Script 1: multiple return
function prueba(valor) {
if (valor == 1) {
return '1';
} else if (valor == 2) {
return '2';
} else if (valor == 3) {
return '3';
}
return '';
}
prueba(3);
Script 2: a single return
function prueba(valor) {
var devolucion = '';
if (valor == 1) {
devolucion = '1';
} else if (valor == 2) {
devolucion = '2';
} else if (valor == 3) {
devolucion = '3';
}
return devolucion;
}
prueba(3);
I do a performance test and it appears to me that the script with a return
unique is faster than the script with multiple return
, as you can see in this image:
Why does that happen? Can someone explain this performance result?