X-Path查询性能

时间:2019-03-15 13:45:35

标签: performance xpath

以下哪种查询被认为是较重的性能:

//*/*/*/*/*

//ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*

/descendant-or-self::*/*/*/*

据我了解,第二个计算起来会更复杂吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

关于执行时间:由于存在很多影响因素,因此没有明显差异,但是每个请求的执行时间几乎相同。 在当前页面上,每个请求的平均执行时间为0.07–0.09µs; 1µs = 1秒* 10 ^ -6。

let arrExecutionTime = [];
function getAverageExecutionTime(locator) {
    let maxIteration = 1000;
    let timeTotal = 0;

    for (let i = 0; i < maxIteration; i++) {
        let executionTimeStart = performance.now();
        let arrNode = document.evaluate(locator, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        arrExecutionTime.push(performance.now() - executionTimeStart);
    }

    for (let k = 0; k < arrExecutionTime.length; k++) {
        timeTotal += arrExecutionTime[k];
    }

    let avgExecutionTime = timeTotal / arrExecutionTime.length;
    console.log(`avgExecutionTime: ${avgExecutionTime}µs \nafter ${maxIteration} iterations \nfor locator:\"${locator}\"`);
}

getAverageExecutionTime("//*/*/*/*/*");
getAverageExecutionTime("//ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*/ancestor-or-self::*");
getAverageExecutionTime("/descendant-or-self::*/*/*/*");