aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/lang/tests/fun_perf.html
blob: 9d19a607878de9e79b317f56efae62f50c13588b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<html>
	<head>
		<title>clocking fun</title>
		<style type="text/css">
			@import "../../../dojo/resources/dojo.css";
		</style>
		<script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug:true"></script>
		<script type="text/javascript" src="../functional.js"></script>
		<script type="text/javascript" src="../functional/sequence.js"></script>
		<script type="text/javascript" src="../functional/fold.js"></script>
		<script type="text/javascript">
			dojo.addOnLoad(function(){
				var LEN = 2000, ITER = 200, b, e, tests = {},
					df = dojox.lang.functional,
					sample = df.repeat(LEN, "+1", 0),
					add = df.lambda("+"),
					isOdd = df.lambda("%2");
					
				var clock = function(body){
					var b = new Date();
					body();
					var e = new Date();
					return e.getTime() - b.getTime();	// in ms
				};
				
				var log = function(name, body){
					b = new Date();
					var ms = clock(body);
					e = new Date();
					console.log(name + ":", ms);
				};

				// filter
				tests["raw filter"] = function(){
					for(var i = 0; i < ITER; ++i){
						var t = [];
						for(var j = 0; j < sample.length; ++j){
							if(isOdd(sample[j])){ t.push(sample[j]); }
						}
					}
				};
				tests["dojo.filter"] = function(){
					for(var i = 0; i < ITER; ++i){
						dojo.filter(sample, isOdd);
					}
				};
				tests["df.filter"] = function(){
					for(var i = 0; i < ITER; ++i){
						df.filter(sample, isOdd);
					}
				};
				if(sample.filter){
					tests["Array.prototype.filter"] = function(){
						for(var i = 0; i < ITER; ++i){
							sample.filter(isOdd);
						}
					};
				}

				// map
				tests["raw map"] = function(){
					for(var i = 0; i < ITER; ++i){
						var t = [];
						for(var j = 0; j < sample.length; ++j){
							t.push(isOdd(sample[j]));
						}
					}
				};
				tests["dojo.map"] = function(){
					for(var i = 0; i < ITER; ++i){
						dojo.map(sample, isOdd);
					}
				};
				tests["df.map"] = function(){
					for(var i = 0; i < ITER; ++i){
						df.map(sample, isOdd);
					}
				};
				if(sample.map){
					tests["Array.prototype.map"] = function(){
						for(var i = 0; i < ITER; ++i){
							sample.map(isOdd);
						}
					};
				}

				// forEach
				tests["raw forEach"] = function(){
					for(var i = 0; i < ITER; ++i){
						for(var j = 0; j < sample.length; ++j){
							isOdd(sample[j]);
						}
					}
				};
				tests["dojo.forEach"] = function(){
					for(var i = 0; i < ITER; ++i){
						dojo.forEach(sample, isOdd);
					}
				};
				tests["df.forEach"] = function(){
					for(var i = 0; i < ITER; ++i){
						df.forEach(sample, isOdd);
					}
				};
				if(sample.forEach){
					tests["Array.prototype.forEach"] = function(){
						for(var i = 0; i < ITER; ++i){
							sample.forEach(isOdd);
						}
					};
				}

				// reduce
				tests["raw reduce"] = function(){
					for(var i = 0; i < ITER; ++i){
						var z = 0;
						for(var j = 0; j < sample.length; ++j){
							z = add(z, sample[j]);
						}
					}
				};
				tests["df.reduce"] = function(){
					for(var i = 0; i < ITER; ++i){
						df.reduce(sample, add, 0);
					}
				};
				if(sample.reduce){
					tests["Array.prototype.reduce"] = function(){
						for(var i = 0; i < ITER; ++i){
							sample.reduce(add, 0);
						}
					};
				}

				// reduceRight
				tests["raw reduceRight"] = function(){
					for(var i = 0; i < ITER; ++i){
						var z = 0;
						for(var j = sample.length - 1; j >= 0; --j){
							z = add(z, sample[j]);
						}
					}
				};
				tests["df.reduceRight"] = function(){
					for(var i = 0; i < ITER; ++i){
						df.reduceRight(sample, add, 0);
					}
				};
				if(sample.reduceRight){
					tests["Array.prototype.reduceRight"] = function(){
						for(var i = 0; i < ITER; ++i){
							sample.reduceRight(add, 0);
						}
					};
				}
				
				var keys = df.keys(tests), i = 0;
				
				var doTest = function(){
					log(keys[i], tests[keys[i]]);
					++i;
					if(i < keys.length){
						setTimeout(doTest, 1);
					}else{
						console.log("that's all");
					}
				};
				
				setTimeout(doTest, 1);
			});
		</script>
	</head>
	<body>
		<p>This test is meant to run with Firebug.</p>
	</body>
</html>