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

It's bad practice to use JS for my software logic? should I use C++ instead?



  • I'm starting to learn QML. At this time, I reached a point where I have to decide between C++ or JS to programming the "model" or "non UI logic" part of my application. This part will consist of some apis queries to obtain information in JSON format or similar. For example weather, google api, news, etc.

    This article discourages the use of JS for the code that is not part of the UI.

    I am more familiar with Javascript than with C++, so it would be more comfortable to use JS.

    If I choose to use JS, is it good practice to put the modules separately or could they be integrated with the QML code?

    I have no problem learning C ++, I just ask what are the recommendations or good practices that are usually followed.



  • I only shared my QML experience. According your goal and situation , you should elect best option...For real time project only use C++, never use JS in your logic and conditional parts. For RAD develop application you can use JS, but you should consider that you will had a little delay. I developed a mini game (with JS and QML) named Qspiral 3 years ago, you can see it's video from this link.

    In windows, little delay didn't cause problem in playing game...
    but for cross platform in android we had crashes...
    Finally for android we had struggle on improve performance and reducing delay...So be careful in choosing the route.



  • Thanks for the response. My application will be a dashboard that will notify new emails, the weather and will be used on desktop.



  • @efdiloreto
    qmlweather example uses unly qml/js



  • @efdiloreto Hello and welcome to the Qt Community ;)

    My advice would be to reduce JS usage to strict minimum and to use C++ to implement business logic.
    I known that JS is an attractive language for beginners, but it is also a very error prone language. You will never know if don't have done typo errors until all the code has been executed and it is between 10 and 100 time slower as C++.
    I've learn it on the hard way, and I have rewritten big parts of my application to move JS logic to C++. This has solve many side effects, like memory management and performances issues.

    To resume my point of view:

    • if you want to do it right, use C++ to business logic,
    • if you want to do something in "quick&dirty style", try to chance with JS

    my 2 cts.



  • Thanks all for the responses. I will try with C++, I think it is a good opportunity to learn, even though the application can be easily resolved with JS.



  • @KroMignon said in It's bad practice to use JS for my software logic? should I use C++ instead?:

    10 and 100 time slower as C++

    Is this really true? Is there a benchmark for this? I looked at JSEngine and cannot tell if it is JIT compiled. I know a JIT compiled engine is not that much slower.







  • @fcarney said in It's bad practice to use JS for my software logic? should I use C++ instead?:

    Is this really true? Is there a benchmark for this? I looked at JSEngine and cannot tell if it is JIT compiled. I know a JIT compiled engine is not that much slower.

    Yes, this is really true, according to my experience with QML/C++.
    In fact, it was with Qt 5.4.2 and Qt 5.7.1, precompiled QML was not available at this time (at least not for open source licence version).

    I do not tell you cannot do it with JS, I just share my experience. JS is a performance killer (execution time and memory usage).
    For me, JS/QML should be used only to handle the user interface logic and C++ should be the "business logic" language.
    This is also the recommendation from Qt, and interfacing QML and C++ is so easy to do!



  • @LeLev Yes, you're right. For make http requests I think that JS is enough.


Log in to reply