Home | History | Annotate | Download | only in dom-perf
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 // Tests cloning and appending small and medium DOM trees.
     32 function CloneNodes(size, style) {
     33     var me = this;
     34     this.size = size;
     35     this.style = style;
     36 
     37     this.name = me.size + ', ' + me.style;
     38 
     39     var nClones = 0;
     40 
     41     this.Setup = function() {
     42         // getDOMTree will initialize the global tree if necessary.
     43         me.domTree = me.getDOMTree();
     44         if (me.style == 'append')
     45             me.domTree = me.domTree.cloneNode(true);
     46     };
     47 
     48     this.Test = function() {
     49         var kIterations = 10;
     50         if (me.style == 'clone') {
     51             for (var iterations = 0; iterations < kIterations; iterations++)
     52                 me.domTree.cloneNode(true);
     53         } else {
     54             for (var iterations = 0; iterations < kIterations; iterations++)
     55                 this.suite.benchmarkContent.appendChild(me.domTree);
     56         }
     57     };
     58 
     59     // Returns a tree of size me.size from the pool in the prototype, creating
     60     // one if needed.
     61     this.getDOMTree = function() {
     62         var domTree = me.Trees[me.size];
     63         if (!domTree) {
     64             switch (this.size) {
     65             case 'small':
     66                 domTree = BenchmarkSuite.prototype.generateSmallTree();
     67                 break;
     68             case 'medium':
     69                 domTree = BenchmarkSuite.prototype.generateMediumTree();
     70                 break;
     71             case 'large':
     72                 domTree = BenchmarkSuite.prototype.generateLargeTree();
     73                 break;
     74             }
     75             me.Trees[me.size] = domTree;
     76         }
     77         return domTree;
     78     };
     79 
     80     this.GetBenchmark = function() {
     81         return new Benchmark(this.name, this.Test, this.Setup);
     82     };
     83 };
     84 
     85 CloneNodes.prototype = {
     86     Sizes: ['small', 'medium'],
     87     Styles: ['clone', 'append'],
     88     Trees: {}
     89 };
     90 
     91 // Generate a test for each size/style combination.
     92 var benchmarks = [];
     93 CloneNodes.prototype.Sizes.forEach(function(size) {
     94     CloneNodes.prototype.Styles.forEach(function(style) {
     95         benchmarks.push(new CloneNodes(size, style).GetBenchmark());
     96     });
     97 });
     98 
     99 var CloneNodesTest = new BenchmarkSuite('CloneNodes', benchmarks);
    100