zoukankan      html  css  js  c++  java
  • DOM walking: Recursive vs. Iterative

    DOM walking: Recursive vs. Iterative · jsPerf

    DOM walking: Recursive vs. Iterative

    JavaScript performance comparison

    Test case created on 18th March 2012

    Preparation code

    <script>
    function domWalkRecursive(root, enter, exit) {
        var enter = enter || function () {},
            exit = exit || function () {},
            node = root;
       
        // Call enter callback, and check if it explicitly returns false
        if (enter && enter(node) === false) {
            // And return false
            return false;
        }
        // Move to first child
        node = node.firstChild;
        // Iterate through sibling nodes
        while (node) {
            // Recurse and check for false return value
            if (domWalkRecursive(node, enter, exit) === false) {
                return false;
            }
            node = node.nextSibling;
        }
        // Call exit callback on the parent node and listen for false return value
        if (exit && exit(root) === false) {
            return false;
        }
    }

    function domWalkIterative(root, enter, exit) {
        var node = root,
            exit = exit || function () {},
            enter = enter || function () {};
       
        start: while(node) {
                enter(node);
                if (node.firstChild) {
                        node = node.firstChild;
                        continue start;
                }
                while (node) {
                        exit(node);
                        if (node === root) {
                            break start;
                    }
                        if (node.nextSibling) {
                                node = node.nextSibling;
                                continue start;
                        }
                        node = node.parentNode;
                }
        }
       
    }
    var output = ['', ''];
    function logger(desc, i) {
        var desc = desc || '',
            i = i || 0;
        return function (node) {
            // Some trivial work to do with node
            output[i] += desc + node.nodeName + '\n';
        };
    }
    </script>
    <div>
    <p>This document contains a sample DOM tree to check tree traversal algorithms
    with. There is an ASCII representation of it below.</p>
    <p>Some sample content follows:</p>
    <ul>
    <li>An unordered list</li>
    <li><ol>
    <li>And a nested ordered list</li>
    <li>...</li>
    </ol></li>
    <li>...</li>
    </ul>
    <div>
    An implicit paragraph
    <img alt="sample image" src="sample.jpg">  blah blah blah.
    <p>And an explicit one</p>
    </div>
    And here is the Tree:
    <pre>
    HTML
        #Text*                                  \n
        HEAD
            #Text*                              \n
            TITLE
                #Text                           Sample Dom Tree
            #Text*                              \n
        #Text*                                  \n
        BODY
            #Text*                              \n
            DIV
                #Text*                          \n
                P
                    #Text                       This document contains a ...
                #Text*                          \n
                P
                    #Text                       Some sample content follows:
                #Text*                          \n
                UL
                    #Text*                      \n
                    LI
                        #Text                   An unordered list
                    #Text*                      \n
                    LI
                        OL
                            #Text*              \n
                            LI
                                #Text           And a nested ordered list
                            #Text*              \n
                            LI
                                #Text           ...
                            #Text*              \n
                    #Text*                      \n
                    LI
                        #Text                   ...
                    #Text*                      \n
                #Text*                          \n
                DIV
                    #Text                       \nAn implicit paragraph\n
                    IMG
                    #Text                       \s\sblah blah blah.\n
                    P
                        #Text                   And an explicit one
                    #Text*                      \n
                #Text                           \nAnd here is the Tree:\n
                PRE
                    #Text                       HTML...
            DIV
            #Text*                              \n
        #Text*                                  \n
    </pre></div>
    <script>
    Benchmark.prototype.teardown = function() {
        if (output[1]) {
            console.log(output[0] === output[1]);
        }
    };
    </script>

    Preparation code output

    This document contains a sample DOM tree to check tree traversal algorithms with. There is an ASCII representation of it below.

    Some sample content follows:

    • An unordered list
      1. And a nested ordered list
      2. ...
    • ...
    An implicit paragraph sample image blah blah blah.

    And an explicit one

    And here is the Tree:
    HTML
        #Text*                                  \n
        HEAD
            #Text*                              \n
            TITLE
                #Text                           Sample Dom Tree
            #Text*                              \n
        #Text*                                  \n
        BODY
            #Text*                              \n
            DIV
                #Text*                          \n
                P
                    #Text                       This document contains a ...
                #Text*                          \n
                P
                    #Text                       Some sample content follows:
                #Text*                          \n
                UL
                    #Text*                      \n
                    LI
                        #Text                   An unordered list
                    #Text*                      \n
                    LI
                        OL
                            #Text*              \n
                            LI
                                #Text           And a nested ordered list
                            #Text*              \n
                            LI
                                #Text           ...
                            #Text*              \n
                    #Text*                      \n
                    LI
                        #Text                   ...
                    #Text*                      \n
                #Text*                          \n
                DIV
                    #Text                       \nAn implicit paragraph\n
                    IMG
                    #Text                       \s\sblah blah blah.\n
                    P
                        #Text                   And an explicit one
                    #Text*                      \n
                #Text                           \nAnd here is the Tree:\n
                PRE
                    #Text                       HTML...
            DIV
            #Text*                              \n
        #Text*                                  \n
    

    Test runner

    Warning! For accurate results, please disable Firebug before running the tests. (Why?)

    Java applet disabled.

    Ready to run.

    Testing in Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.3) Gecko/20121108 Firefox/15.3 PaleMoon/15.3
    Test Ops/sec
    Recursive
    output[0] = '';
    domWalkRecursive(document.documentElement, logger('entering ', 0), logger('exiting ', 0));
    ready
    Iterative
    output[1] = '';
    domWalkIterative(document.documentElement, logger('entering ', 1), logger('exiting ', 1));
    ready

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

  • 相关阅读:
    Maidsafe-去中心化互联网白皮书
    The Top 20 Cybersecurity Startups To Watch In 2021 Based On Crunchbase
    Top 10 Blockchain Security and Smart Contract Audit Companies
    The 20 Best Cybersecurity Startups To Watch In 2020
    Blockchain In Cybersecurity: 11 Startups To Watch In 2019
    004-STM32+BC26丨260Y基本控制篇(阿里云物联网平台)-在阿里云物联网平台上一型一密动态注册设备(Android)
    涂鸦开发-单片机+涂鸦模组开发+OTA
    000-ESP32学习开发-ESP32烧录板使用说明
    03-STM32+Air724UG远程升级篇OTA(阿里云物联网平台)-STM32+Air724UG使用阿里云物联网平台OTA远程更新STM32程序
    03-STM32+Air724UG远程升级篇OTA(自建物联网平台)-STM32+Air724UG实现利用http/https远程更新STM32程序(TCP指令,单片机程序检查更新)
  • 原文地址:https://www.cnblogs.com/lexus/p/2821097.html
Copyright © 2011-2022 走看看