3

I'm attempting to allow user defined logic criteria. Since the user definitions are basically strings, I'm trying to make the code efficient by avoiding string comparison. For example, a simple test like a == 10 would be represented by an array

var userDef = ['a', '==', 10]

To make the logic evaluation efficient, I'd like to create an object like the ffg:

var binaryOperator = {'==': 0, '>': 1, '<': 2}

.. so when I mine the array, I could do e.g.

if(binaryOperator[userdef[1]] == 0)
{
    return (obj[userDef[0]] == userDef[2])
}

... where of course obj.a = something. The above code, on the face of it, avoids string comparison.

But does it really? Is the reference binaryOperator[userdef[1]] really fast or does it also involve string comparison somewhere?

In other words is there a performance difference between thisObj.a and thisObj['a']?

Roman
  • 4,922
  • 3
  • 22
  • 31
Randhir Rawatlal
  • 365
  • 1
  • 3
  • 13
  • this line has syntax error `(obj[userDef[0] == userDef[2])` – gurvinder372 Dec 05 '17 at 08:21
  • Yes I corrected I think just as you commented on that, but thanks – Randhir Rawatlal Dec 05 '17 at 08:22
  • why not write `if( userdef[1] == '==' )`, or `switch( userdef[i] ){ case "==" }...` – gurvinder372 Dec 05 '17 at 08:23
  • where do you know if the operand is an accessor for the object? – Nina Scholz Dec 05 '17 at 08:23
  • well that would definitely involve string comparison which is less efficient than comparing integers. I'm trying to reduce my strings to integers so the logic evaluation will go faster. – Randhir Rawatlal Dec 05 '17 at 08:25
  • @Nina This is part of class where some of those things will be handled. In this question i've just cut out the part for this question – Randhir Rawatlal Dec 05 '17 at 08:28
  • So, you are replacing string comparison with map access and integer comparison. Not sure why you are worried about performance of string comparison, that too of such small string `==`. – gurvinder372 Dec 05 '17 at 08:28
  • Is there a performance difference between thisObj.a and thisObj['a']? 1.Square bracket notation allows the use of characters that can't be used with dot notation. 2.square bracket notation is useful when dealing with variable name. 3.you can use square bracket when property names are dynamically determined.to use dot you must property name 4.square bracket allows properties containing special characters. – Neeraj Goswami Dec 05 '17 at 08:31
  • Thanks Neeraj, your point 3 implies that square bracket notation will involve string comparison, which would answer the question (which is to say it would not be as efficient.. in spite of all the advantages you're citing..) – Randhir Rawatlal Dec 05 '17 at 08:50

1 Answers1

2

You could omit the check for the operator and take a function as value, like

var binaryOperator = {
        '==': function (a, b) { return a === b; },
        '<': function (a, b) { return a < b; },
        '>': function (a, b) { return a > b; }
    };

Objects in Javascript have an access of O(1).

Further readings:

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I really like that - although it possibly has the same question - using binaryOperator.== is legal, so is binaryOperator['=='] as efficient? – Randhir Rawatlal Dec 05 '17 at 08:31
  • it is, because objects have an access of O(1). – Nina Scholz Dec 05 '17 at 08:32
  • Well this last comment of yours is starting to answer the question. If you could write that in as an answer together with perhaps some refs to the fact that the obj['attribute'] notation is O(1) I would mark it as accepted. Remember my basic concern is that fact that using a string in the object attribute accessor might involve string comparison itself. – Randhir Rawatlal Dec 05 '17 at 08:42