3

I have following code

function MyFunc() {

    var add = function (props) {

      if (props.hasOwnProperty('a') && props.hasOwnProperty('b')) {
        return 'ab';
      } else if (props.hasOwnProperty('c')) {
        return 'c';
      } else if (props.hasOwnProperty('d')) {
        return 'd';
      } else {
        throw new Error("Doomed!!!");
      }

    };

    var div = function () {
        return "Hello from div";
    };

    var methods = {
        add: add,
        div: div
    };

    var funcCall = function (obj) {

        if (!obj) {
            throw new Error("no Objects are passed");
        }

        return methods[obj.fName](obj.props);
    };

    return {
        func: function (obj) {
            return funcCall(obj);
        }
    };

}

var lol = new MyFunc();

When lol.func({fName: 'add', props: {a: 'a', b: 'b'}}); is run it should return the correct response from add functions inner if else statements. But there can be more than 20 else if occurrences. My question is will this be a reason for bad performance, Is there any alternative approch for achieving this

DEMO

UPDATE

Another question

Could someone please explain me how to implement map based conditioning for this code

It worked yesterday.
  • 4,507
  • 11
  • 46
  • 81

5 Answers5

2

You could use a switch statement but more complex logic such as && starts to get more complicated (they are really designed for just quick one to one comparisons). I would stick with what you have if you want that more complex logic. It is technically the slowest but if all the other ways are very complicated to implement the gain in performance will not be worth it.

carloabelli
  • 4,289
  • 3
  • 43
  • 70
1

you can use switch statements http://www.w3schools.com/js/js_switch.asp

Anit
  • 174
  • 5
1

My question is will this be a reason for bad performance

Well in all cases the logic show that you need to do some check before returning the name of these properties. So If you care about performance you need to decrease the time of each check.

What you could do is retrieve the list of props properties instead of doing hasOwnProperty in each if statements, and use the list to do the check with indexOf.

var propsList = Object.keys(props);

if (propsList.indexOf("a") > 0 && propsList.indexOf("b") > 0){
    return "ab"; 
}

With this if you want avoid if statements, you could use a for loop and an Array that will contain all the properties

var add = function (props) {
    var listKeys = ["ab", "c", "d"]; // List of the properties

    var objectKeys = Object.keys(props);
    for (var i = 0, len = listKeys.length; i < len ; i++){
        var listProps = listKeys[i].split("");

        var ok = true;
        for (var j = 0, len2 = listProps.length; j < len2 ; j++){
            if (objectKeys.indexOf(listProps[j]) < 0){
                ok = false;
                break;
            }
        }
        if (ok){
           return listKeys[i];
        }
    }

    throw new Error("Doomed!!!"); 
}
Hacketo
  • 4,978
  • 4
  • 19
  • 34
1

I have experimented a more generic solution as follows:

function MyFunc() {

    // Check if all 'searchedProps[]' properties are contained in 'obj';
    // If one property is not found it will return false;
    // If all properties are found it will return true;
    function hasProperties(obj, searchedProps) {
        for (var i = 0; i < searchedProps.length; i++) {
            if (!obj.hasOwnProperty(searchedProps[i])) {
                return false;
            }
        }
        return true;
    }

    var add = function (props) {

        var options = [
            {
                properties: ["a", "b"],
                result: "ab"
            },
            {
                properties: ["c"],
                result: "c"
            },
            {
                properties: ["d"],
                result: "d"
            }];

        for (var i = 0; i < options.length; i++) {
            if (hasProperties(props, options[i].properties)) {
                return options[i].result;
            }
        }

        throw new Error("Doomed!!!");

    };

    var div = function () {
        return "Hello from div";
    };

    var methods = {
        add: add,
        div: div
    };

    var funcCall = function (obj) {

        if (!obj) {
            throw new Error("no Objects are passed");
        }

        return methods[obj.fName](obj.props);
    };

    return {
        func: function (obj) {
            return funcCall(obj);
        }
    };

}

var lol = new MyFunc();

var result = lol.func({ fName: 'add', props: { a: 'a', b: 'b' } });

console.log(result);
Please check the result in console.
marianc
  • 439
  • 1
  • 4
  • 13
-1

You could try to use Swtich case (http://www.w3schools.com/js/js_switch.asp) Don't know if this will work in your case.

switch(probs){
      case "(props.hasOwnProperty('c'))"
           return 'c';
      case "(props.hasOwnProperty('d'))"
           return 'd'; }

should look like this at the end

alovaros
  • 476
  • 4
  • 23