samedi 28 novembre 2015

Javascript : Speed between "if" and "object"? [on hold]

I was wondering what was faster between an "if" and a "json" access

Just consedering this :

var val1 = "IAM1",
    val2 = "IAM2",
    val3 = "IAM3",
    val4 = "IAM4",
    myVal = null,
    obj = {};

obj[val1][val2][val3] = val4;

I want to set quickly myVal :

// First Case
myVal = obj[val1][val2][val3];

// Second case
var set = function(value) {
   if (value == val1) myVal = val1;
   if (value == val2) myVal = val2;
   if (value == val3) myVal = val3;
   if (value == val4) myVal = val4;
}
set(val4);

Knowing that "obj" can have even more elements or/and that the "set" method can be called by other objects multiple time

What is the faster way to set "IAM4" on myVal ?

Thanks !

Aucun commentaire:

Enregistrer un commentaire