source: Dev/trunk/src/client/dojox/lang/tests/fun_perf.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.0 KB
Line 
1<html>
2        <head>
3                <title>Clocking fun</title>
4                <style type="text/css">
5                        @import "../../../dojo/resources/dojo.css";
6                </style>
7                <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug:true"></script>
8                <script type="text/javascript" src="../functional.js"></script>
9                <script type="text/javascript" src="../functional/sequence.js"></script>
10                <script type="text/javascript" src="../functional/fold.js"></script>
11                <script type="text/javascript">
12                        var clock = function(body){
13                                var b = new Date();
14                                body();
15                                var e = new Date();
16                                return e.getTime() - b.getTime();       // in ms
17                        };
18                       
19                        var log = function(name, body){
20                                var ms = clock(body);
21                                console.log(name + ":", ms);
22                        };
23
24                        var LEN = 2000, ITER = 200, tests = {},
25                                df = dojox.lang.functional,
26                                sample = df.repeat(LEN, "+1", 0),
27                                add = df.lambda("+"),
28                                isOdd = df.lambda("%2");
29                               
30                        // filter
31                        tests["raw filter"] = function(){
32                                for(var i = 0; i < ITER; ++i){
33                                        var t = [];
34                                        for(var j = 0; j < sample.length; ++j){
35                                                if(isOdd(sample[j])){ t.push(sample[j]); }
36                                        }
37                                }
38                        };
39                        tests["dojo.filter"] = function(){
40                                for(var i = 0; i < ITER; ++i){
41                                        dojo.filter(sample, isOdd);
42                                }
43                        };
44                        tests["df.filter"] = function(){
45                                for(var i = 0; i < ITER; ++i){
46                                        df.filter(sample, isOdd);
47                                }
48                        };
49                        if(sample.filter){
50                                tests["Array.prototype.filter"] = function(){
51                                        for(var i = 0; i < ITER; ++i){
52                                                sample.filter(isOdd);
53                                        }
54                                };
55                        }
56
57                        // map
58                        tests["raw map"] = function(){
59                                for(var i = 0; i < ITER; ++i){
60                                        var t = [];
61                                        for(var j = 0; j < sample.length; ++j){
62                                                t.push(isOdd(sample[j]));
63                                        }
64                                }
65                        };
66                        tests["dojo.map"] = function(){
67                                for(var i = 0; i < ITER; ++i){
68                                        dojo.map(sample, isOdd);
69                                }
70                        };
71                        tests["df.map"] = function(){
72                                for(var i = 0; i < ITER; ++i){
73                                        df.map(sample, isOdd);
74                                }
75                        };
76                        if(sample.map){
77                                tests["Array.prototype.map"] = function(){
78                                        for(var i = 0; i < ITER; ++i){
79                                                sample.map(isOdd);
80                                        }
81                                };
82                        }
83
84                        // forEach
85                        tests["raw forEach"] = function(){
86                                for(var i = 0; i < ITER; ++i){
87                                        for(var j = 0; j < sample.length; ++j){
88                                                isOdd(sample[j]);
89                                        }
90                                }
91                        };
92                        tests["dojo.forEach"] = function(){
93                                for(var i = 0; i < ITER; ++i){
94                                        dojo.forEach(sample, isOdd);
95                                }
96                        };
97                        tests["df.forEach"] = function(){
98                                for(var i = 0; i < ITER; ++i){
99                                        df.forEach(sample, isOdd);
100                                }
101                        };
102                        if(sample.forEach){
103                                tests["Array.prototype.forEach"] = function(){
104                                        for(var i = 0; i < ITER; ++i){
105                                                sample.forEach(isOdd);
106                                        }
107                                };
108                        }
109
110                        // reduce
111                        tests["raw reduce"] = function(){
112                                for(var i = 0; i < ITER; ++i){
113                                        var z = 0;
114                                        for(var j = 0; j < sample.length; ++j){
115                                                z = add(z, sample[j]);
116                                        }
117                                }
118                        };
119                        tests["df.reduce"] = function(){
120                                for(var i = 0; i < ITER; ++i){
121                                        df.reduce(sample, add, 0);
122                                }
123                        };
124                        if(sample.reduce){
125                                tests["Array.prototype.reduce"] = function(){
126                                        for(var i = 0; i < ITER; ++i){
127                                                sample.reduce(add, 0);
128                                        }
129                                };
130                        }
131
132                        // reduceRight
133                        tests["raw reduceRight"] = function(){
134                                for(var i = 0; i < ITER; ++i){
135                                        var z = 0;
136                                        for(var j = sample.length - 1; j >= 0; --j){
137                                                z = add(z, sample[j]);
138                                        }
139                                }
140                        };
141                        tests["df.reduceRight"] = function(){
142                                for(var i = 0; i < ITER; ++i){
143                                        df.reduceRight(sample, add, 0);
144                                }
145                        };
146                        if(sample.reduceRight){
147                                tests["Array.prototype.reduceRight"] = function(){
148                                        for(var i = 0; i < ITER; ++i){
149                                                sample.reduceRight(add, 0);
150                                        }
151                                };
152                        }
153                       
154                        var keys = df.keys(tests), i = 0;
155                       
156                        var doTest = function(){
157                                log(keys[i], tests[keys[i]]);
158                                ++i;
159                                if(i < keys.length){
160                                        setTimeout(doTest, 20);
161                                }else{
162                                        console.log("that's all");
163                                }
164                        };
165                       
166                        var test = function(){
167                                i = 0;
168                                setTimeout(doTest, 20);
169                        };
170                               
171                        //dojo.addOnLoad(test);
172                </script>
173        </head>
174        <body>
175                <p>This test is meant to run with Firebug. Open the console to see the output.</p>
176                <p><button onclick="test()">Start</button></p>
177        </body>
178</html>
Note: See TracBrowser for help on using the repository browser.