Skip to content

taskmanager: Use simpler regex to process the title

Fushan Wen requested to merge work/fusionfuture/tooltip-one-line-regex into master

Some minor optimizations

Test result

  • before: 2590ms
  • after: 1262ms
import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2
import QtQuick.Window 2.15

Window {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("~:bash — Konsole<2>")

    QQC2.Button {
        text: "Run"
        onClicked: root.run()
    }

    function run() {
        let text = "";
        const testnum = 1000000, appName = "konsole";

        console.time("before");

        for (let i = 0; i < testnum; i++) {
            const counter = title.match(/<\d+>\W*$/);
            text = title.replace(/\s*<\d+>\W*$/, "");

            const appNameRegex = new RegExp(appName + "$", "i");
            text = text.replace(appNameRegex, "");
            text = text.replace(/\s*(?:-|—)*\s*$/, "");

            if (counter !== null) {
                if (text === "") {
                    text = counter;
                } else {
                    text = text + " " + counter;
                }
            }
        }

        console.timeEnd("before");

        console.time("after");

        for (let i = 0; i < testnum; i++) {
            text = `${(title.match(/.*(?=\s*?)/) || [""])[0]}${(title.match(/<\d+>/) || [""]).pop()}`;
        }

        console.timeEnd("after");
    }
}
Edited by Fushan Wen

Merge request reports