OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 function ObjectWithKeys(count, keyOffset, keyGen) { | 5 function ObjectWithKeys(count, keyOffset, keyGen) { |
6 if (keyOffset === undefined) keyOffset = 0; | 6 if (keyOffset === undefined) keyOffset = 0; |
7 if (keyGen === undefined) keyGen = (i) => { return "key" + i }; | 7 if (keyGen === undefined) keyGen = (i) => { return "key" + i }; |
8 var o = {}; | 8 var o = {}; |
9 for (var i = 0; i < count; i++) { | 9 for (var i = 0; i < count; i++) { |
10 var key = keyGen(i + keyOffset); | 10 var key = keyGen(i + keyOffset); |
11 o[key] = "value"; | 11 o[key] = "value"; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 for (var i = 0; i < size; i++) { | 51 for (var i = 0; i < size; i++) { |
52 array[i] = i; | 52 array[i] = i; |
53 } | 53 } |
54 return array; | 54 return array; |
55 } | 55 } |
56 | 56 |
57 // ============================================================================ | 57 // ============================================================================ |
58 var object_empty = {}; | 58 var object_empty = {}; |
59 var array_empty = []; | 59 var array_empty = []; |
60 | 60 |
61 var array_int_100 = IntArray(100); | 61 var array_int_50 = IntArray(50); |
62 var array_int_100_proto_elements = IntArray(100); | 62 var array_int_50_proto_elements = IntArray(50); |
63 array_int_100_proto_elements.__proto__ = [101, 102, 103, 104]; | 63 array_int_50_proto_elements.__proto__ = [51, 52, 53, 54]; |
64 var array_int_holey_100 = HoleyIntArray(100); | 64 var array_int_holey_50 = HoleyIntArray(50); |
65 | 65 |
66 var empty_proto_5_10 = ObjectWithKeys(5); | 66 var empty_proto_5_10 = ObjectWithKeys(5); |
67 empty_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0); | 67 empty_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0); |
68 | 68 |
69 var empty_proto_5_5_slow = ObjectWithKeys(5); | 69 var empty_proto_5_5_slow = ObjectWithKeys(5); |
70 empty_proto_5_5_slow.__proto__ = ObjectWithProtoKeys(5, 0, false); | 70 empty_proto_5_5_slow.__proto__ = ObjectWithProtoKeys(5, 0, false); |
71 | 71 |
72 var object_elements_proto_5_10 = ObjectWithKeys(5); | 72 var object_elements_proto_5_10 = ObjectWithKeys(5); |
73 object_elements_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0); | 73 object_elements_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0); |
74 // Add some properties further up the prototype chain, the rest stays | 74 // Add some properties further up the prototype chain, the rest stays |
75 // empty. | 75 // empty. |
76 for (var i = 0; i < 5; i++) { | 76 for (var i = 0; i < 5; i++) { |
77 object_elements_proto_5_10.__proto__.__proto__.__proto__["proto" + i] = true; | 77 object_elements_proto_5_10.__proto__.__proto__.__proto__["proto" + i] = true; |
78 } | 78 } |
79 | 79 |
80 var TestObjects = { | 80 var TestObjects = { |
81 object_empty: object_empty, | 81 object_empty: object_empty, |
82 array_empty: array_empty, | 82 array_empty: array_empty, |
83 array_int_100: array_int_100, | 83 array_int_50: array_int_50, |
84 array_int_holey_100: array_int_holey_100, | 84 array_int_holey_50: array_int_holey_50, |
85 array_int_100_proto_elements: array_int_100_proto_elements, | 85 array_int_50_proto_elements: array_int_50_proto_elements, |
86 empty_proto_5_10: empty_proto_5_10, | 86 empty_proto_5_10: empty_proto_5_10, |
87 empty_proto_5_5_slow: empty_proto_5_5_slow, | 87 empty_proto_5_5_slow: empty_proto_5_5_slow, |
88 object_elements_proto_5_10: object_elements_proto_5_10 | 88 object_elements_proto_5_10: object_elements_proto_5_10 |
89 } | 89 } |
90 | 90 |
91 var TestArrays = | 91 var TestArrays = { |
92 { | 92 array_empty: array_empty, |
93 array_empty: array_empty, | 93 array_int_50: array_int_50, |
94 array_int_100: array_int_100, | 94 array_int_holey_50: array_int_holey_50, |
95 array_int_holey_100: array_int_holey_100, | 95 array_int_50_proto_elements: array_int_50_proto_elements, |
96 array_int_100_proto_elements: array_int_100_proto_elements | 96 } |
97 } | |
98 | 97 |
99 // ============================================================================ | 98 // ============================================================================ |
100 | 99 |
101 function CreateTestFunctionGen(fn) { | 100 function CreateTestFunctionGen(fn) { |
102 // Force a new function for each test-object to avoid side-effects due to ICs. | 101 // Force a new function for each test-object to avoid side-effects due to ICs. |
103 return (object) => { | 102 return (object) => { |
104 var random_comment = "\n// random comment" + Math.random() + "\n"; | 103 var random_comment = "\n// random comment" + Math.random() + "\n"; |
105 return eval(random_comment + fn.toString()); | 104 return eval(random_comment + fn.toString()); |
106 } | 105 } |
107 } | 106 } |
108 | 107 |
109 var TestFunctions = { | 108 var TestFunctions = { |
110 "Object.keys()": CreateTestFunctionGen(() => {return Object.keys(object)}), | 109 "Object.keys()": CreateTestFunctionGen(() => {return Object.keys(object)}), |
111 "for (in)": CreateTestFunctionGen(() => { | 110 "for-in": CreateTestFunctionGen(() => { |
112 var count = 0; | 111 var count = 0; |
113 var result; | 112 var result; |
114 for (var key in object) { | 113 for (var key in object) { |
115 count++; | 114 count++; |
116 result = object[key]; | 115 result = object[key]; |
117 }; | 116 }; |
118 return [result, count]; | 117 return [result, count]; |
119 }), | 118 }), |
120 "for (in) hasOwnProperty()": CreateTestFunctionGen(() => { | 119 "for-in hasOwnProperty()": CreateTestFunctionGen(() => { |
121 var count = 0; | 120 var count = 0; |
122 var result; | 121 var result; |
123 for (var key in object) { | 122 for (var key in object) { |
124 if (!object.hasOwnProperty(key)) continue; | 123 if (!object.hasOwnProperty(key)) continue; |
125 count++; | 124 count++; |
126 result = object[key]; | 125 result = object[key]; |
127 }; | 126 }; |
128 return [result, count]; | 127 return [result, count]; |
129 }), | 128 }), |
130 "for (i < Object.keys().length)": CreateTestFunctionGen(() => { | 129 "for (i < Object.keys().length)": CreateTestFunctionGen(() => { |
131 var count = 0; | 130 var count = 0; |
132 var result; | 131 var result; |
133 var keys = Object.keys(object) for (var i = 0; i < keys.length; i++) { | 132 var keys = Object.keys(object); |
| 133 for (var i = 0; i < keys.length; i++) { |
134 count++; | 134 count++; |
135 result = object[keys[i]]; | 135 result = object[keys[i]]; |
136 }; | 136 }; |
137 return [result, count]; | 137 return [result, count]; |
138 }), | 138 }), |
139 "Object.keys().forEach()": CreateTestFunctionGen(() => { | 139 "Object.keys().forEach()": CreateTestFunctionGen(() => { |
140 var count = 0; | 140 var count = 0; |
141 var result; | 141 var result; |
142 Object.keys(object).forEach((value, index, obj) => { | 142 Object.keys(object).forEach((value, index, obj) => { |
143 count++; | 143 count++; |
(...skipping 19 matching lines...) Expand all Loading... |
163 var length = object.length; | 163 var length = object.length; |
164 for (var i = 0; i < length; i++) { | 164 for (var i = 0; i < length; i++) { |
165 count++; | 165 count++; |
166 result = object[i]; | 166 result = object[i]; |
167 }; | 167 }; |
168 return [result, count]; | 168 return [result, count]; |
169 }) | 169 }) |
170 } | 170 } |
171 | 171 |
172 // ============================================================================ | 172 // ============================================================================ |
173 | 173 // Create the benchmark suites. We create a suite for each of the test |
| 174 // functions above and each suite contains benchmarks for each object type. |
174 var Benchmarks = []; | 175 var Benchmarks = []; |
175 | 176 |
176 function NewBenchmark( | 177 function NewBenchmark( |
177 test_function_gen, test_function_name, test_object, test_object_name) { | 178 test_function_gen, test_function_name, test_object, test_object_name) { |
178 var object = test_object; | 179 var object = test_object; |
179 var name = test_function_name + " " + test_object_name; | 180 var name = test_function_name + " " + test_object_name; |
180 var test_function = test_function_gen(object); | 181 var test_function = test_function_gen(object); |
181 return new Benchmark(name, false, false, 0, test_function) | 182 return new Benchmark(name, false, false, 0, test_function) |
182 } | 183 } |
183 | 184 |
184 for (var test_function_name in TestFunctions) { | 185 for (var test_function_name in TestFunctions) { |
185 var test_function_gen = TestFunctions[test_function_name]; | 186 var test_function_gen = TestFunctions[test_function_name]; |
186 var benchmarks = []; | 187 var benchmarks = []; |
187 for (var test_object_name in TestObjects) { | 188 for (var test_object_name in TestObjects) { |
188 var test_object = TestObjects[test_object_name]; | 189 var test_object = TestObjects[test_object_name]; |
189 var benchmark = NewBenchmark( | 190 var benchmark = NewBenchmark( |
190 test_function_gen, test_function_name, test_object, test_object_name); | 191 test_function_gen, test_function_name, test_object, test_object_name); |
191 benchmarks.push(benchmark); | 192 benchmarks.push(benchmark); |
192 } | 193 } |
193 Benchmarks.push(new BenchmarkSuite(test_function_name, [1000], benchmarks)); | 194 Benchmarks.push(new BenchmarkSuite(test_function_name, [100], benchmarks)); |
194 } | 195 } |
195 | 196 |
196 for (var test_function_name in TestFunctionsArrays) { | 197 for (var test_function_name in TestFunctionsArrays) { |
197 var test_function_gen = TestFunctionsArrays[test_function_name]; | 198 var test_function_gen = TestFunctionsArrays[test_function_name]; |
198 var benchmarks = []; | 199 var benchmarks = []; |
199 for (var test_array_name in TestArrays) { | 200 for (var test_array_name in TestArrays) { |
200 var test_array = TestArrays[test_array_name]; | 201 var test_array = TestArrays[test_array_name]; |
201 var benchmark = NewBenchmark( | 202 var benchmark = NewBenchmark( |
202 test_function_gen, test_function_name, test_array, test_array_name); | 203 test_function_gen, test_function_name, test_array, test_array_name); |
203 benchmarks.push(benchmark); | 204 benchmarks.push(benchmark); |
204 } | 205 } |
205 Benchmarks.push(new BenchmarkSuite(test_function_name, [1000], benchmarks)); | 206 Benchmarks.push(new BenchmarkSuite(test_function_name, [100], benchmarks)); |
206 } | 207 } |
207 | 208 |
208 // ============================================================================ | 209 // ============================================================================ |
OLD | NEW |