Last active
January 7, 2020 15:03
-
-
Save bjorn/349d1b409636bdbf4afb0888d4d2e03a to your computer and use it in GitHub Desktop.
QJSEngine and QStringList properties handling, a potential performance issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QDebug> | |
#include <QQmlApplicationEngine> | |
class Object : public QObject | |
{ | |
Q_OBJECT | |
Q_PROPERTY(QStringList strings READ strings CONSTANT) | |
public: | |
QStringList strings() const | |
{ | |
static unsigned callCounter = 0; | |
qDebug() << Q_FUNC_INFO << "called" << ++callCounter << "times"; | |
QStringList ret; | |
for (int i = 0; i < 10; ++i) | |
ret.append(QString::number(i)); | |
return ret; | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication app(argc, argv); | |
QJSEngine engine; | |
engine.installExtensions(QJSEngine::ConsoleExtension); | |
qmlRegisterUncreatableType<Object>("App", 1, 0, "Object", QStringLiteral("Use 'object'")); | |
engine.globalObject().setProperty(QStringLiteral("object"), engine.newQObject(new Object)); | |
// Note how the following snippet causes Object::strings to be called | |
// 2 times + as many times as there are items in the list. | |
engine.evaluate(QStringLiteral("console.log(object.strings)")); | |
} | |
#include "main.moc" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QStringList Object::strings() const called 1 times | |
QStringList Object::strings() const called 2 times | |
QStringList Object::strings() const called 3 times | |
QStringList Object::strings() const called 4 times | |
QStringList Object::strings() const called 5 times | |
QStringList Object::strings() const called 6 times | |
QStringList Object::strings() const called 7 times | |
QStringList Object::strings() const called 8 times | |
QStringList Object::strings() const called 9 times | |
QStringList Object::strings() const called 10 times | |
QStringList Object::strings() const called 11 times | |
QStringList Object::strings() const called 12 times | |
js: [0,1,2,3,4,5,6,7,8,9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment