mmod

Benchmark created on


Preparation HTML

<script>

const bigData = new Array(10).fill(0).map((_, idx) => {
    const children = new Array(900).fill(0).map((__, iidx) => ({
        label: `label-${idx}-${iidx}`,
        value: `value-${idx}-${iidx}`,
        children: [],
    }));
    return {
        value: `value-${idx}`,
        label: `label-${idx}`,
        children,
    };
});
function batchGetLeafNodes(nodes, childrenKey = 'children') {
    const leafNodes = [];
    const traverse = (node) => {
        if (node.isLeaf || !node[childrenKey].length) {
            // 证明是叶子结点
            leafNodes.push(node);
        } else {
            node[childrenKey].forEach(traverse);
        }
    };
    nodes.forEach(traverse);
    return leafNodes;
}
function getRooMultiCascaderValueByLeafValue(
    nodes,
    leafValues,
    options,
) {
    const {
        valueKey = 'value', childrenKey = 'children', isAllLeafValue = false, isFirstLeafValue = false,
    } = options || {};

    if (!(isAllLeafValue || isFirstLeafValue) && (!Array.isArray(leafValues) || !leafValues.length)) {
        return [];
    }
    const result = [];

    const traverse = (node) => {
        const currentValue = node[valueKey];

        if (node.isLeaf || !node[childrenKey].length) {
            // 叶子节点
            if (leafValues.includes(currentValue)) {
                result.push(currentValue);
            }
        } else {
            // 非叶子节点
            const leafs = batchGetLeafNodes(node[childrenKey]).map(
                (item) => item[valueKey],
            );
            if (leafs.every((item) => leafValues.includes(item))) {
                // 该节点的所有叶子节点都在里面,则 push currentValue
                result.push(currentValue);
            } else {
                // 不全在,则 traverse children的所有节点
                node[childrenKey].forEach((item) => traverse(item));
            }
        }
    };

    const traverseFirst = (node) => {
        if (node.isLeaf || !node[childrenKey].length) {
            // 叶子节点
            result.push(node[valueKey]);
        } else {
            node[childrenKey].forEach((item) => traverseFirst(item));
        }
    };

    if (isAllLeafValue) {
        return nodes.map((item) => item[valueKey]);
    }
    if (isFirstLeafValue) {
        traverseFirst(nodes[0]);
        return result;
    }

    nodes.forEach((node) => traverse(node));
    return result;
}

function test1() {
    const allLeaf = batchGetLeafNodes(bigData)
    const val = getRooMultiCascaderValueByLeafValue(bigData, allLeaf);
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
test1()
test1()
ready
test1()
test1()
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.