Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What's faster: JS comparison vs C++ bool



  • Hi,
    To make this question clear enough I need to describe it more,
    but long story short at first:

    if (GlobalObject.someClass.type === SomeClass.SomeType)
    

    vs

    if (GlobalObject.someClass.someType)
    

    Which one call is better (faster, clearer, proper)?

    ... and to continue explanation:
    SomeClass is QGadget - it has to be light:

    class SomeClass
    {
      Q_GADGET
    
       Q_PROPERTY(EnumType type READ type)
       Q_PROPERTY(bool someType READ someType)
      // etc... for all enumerator types
    
    public:
      SomeClass();
      enum EnumType {
        SomeType, OtherType, YetAnotherType
      }
      Q_ENUM(EnumType)
    
      EnumType type() const { return m_type; }
    
      bool someType() const { return m_type == SomeType; }
      // etc... for all enumerator types
    
    private:
      EnumType m_type;
    }
    

    And GlobalObject is QObject derivative available in all QML code and has someClass property (member)

    Does it make any difference, what do you think?


  • Moderators

    @SeeLook hard to tell without benchmarking it

    I would say if (GlobalObject.someClass.type === SomeClass.SomeType) is slower, as the JS-Engine does not do inlining and this has 2 calls more than if (GlobalObject.someClass.someType)


    actually, modern JavaScript does indeed do inlining, huh, you learn something new every time.
    But as far as I can tell, its v8 or later and Qt is using v7 -> QML does no inlining



  • @J-Hilk Thank You for the answer.

    For me if (GlobalObject.someClass.someType) is more neat as well,
    so +2 for it :-)



  • In fact, there is one more option for this riddle:

    if (GlobalObject.someClass.type === 0) // SomeClass.SomeType
    

    and that comment is mandatory part of the code.

    It makes one call less but it is clumsy.


Log in to reply