Chromium Code Reviews

Side by Side Diff: test/js-perf-test/Keys/keys.js

Issue 1702613002: [js-perf-test] Adding micro benchmarks for for-in and keys patterns. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « test/js-perf-test/JSTests.json ('k') | test/js-perf-test/Keys/run.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 function ObjectWithKeys(count, keyOffset, keyGen) {
6 if (keyOffset === undefined) keyOffset = 0;
7 if (keyGen === undefined) keyGen = (i) => { return "key" + i } var o = {};
Toon Verwaest 2016/02/29 12:19:07 newline before var o = {} at the end.
8 for (var i = 0; i < count; i++) {
9 var key = keyGen(i + keyOffset);
10 o[key] = "value";
11 }
12 return o
13 }
14
15 function ObjectWithMixedKeys(count, keyOffset) {
16 return ObjectWithKeys(count, keyOffset, (key) => {
17 if (key % 2 == 0) return key;
18 return "key" + key;
19 });
20 }
21
22 // Create an object with #depth prototypes each having #keys properties.
23 function ObjectWithProtoKeys(depth, keys, cacheable) {
24 var o = ObjectWithKeys(keys);
25 var current = o;
26 var keyOffset = 0;
27 for (var i = 0; i < depth; i++) {
28 keyOffset += keys;
29 current.__proto__ = ObjectWithKeys(keys, keyOffset);
30 current = current.__proto__;
31 }
32 if (cacheable === false) {
33 // Add an empty proxy at the prototype chain to make caching properties
34 // impossible.
35 current.__proto__ = new Proxy({}, {});
36 }
37 return o;
38 }
39
40 function HoleyIntArray(size) {
41 var array = new Array(size);
42 for (var i = 0; i < size; i += 3) {
43 array[i] = i;
44 }
45 return array
46 }
47
48 function IntArray(size) {
49 var array = new Array(size);
50 for (var i = 0; i < size; i++) {
51 array[i] = i;
52 }
53 return array;
54 }
55
56 // ============================================================================
57 var object_empty = {};
58 var array_empty = [];
59
60 var array_int_100 = IntArray(100);
61 var array_int_100_proto_elements = IntArray(100);
62 array_int_100_proto_elements.__proto__ = [101, 102, 103, 104];
63 var array_int_holey_100 = HoleyIntArray(100);
64
65 var empty_proto_5_10 = ObjectWithKeys(5);
66 empty_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0);
67
68 var empty_proto_5_5_slow = ObjectWithKeys(5);
69 empty_proto_5_5_slow.__proto__ = ObjectWithProtoKeys(5, 0, false);
70
71 var object_elements_proto_5_10 = ObjectWithKeys(5);
72 object_elements_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0);
73 // Add some properties further up the prototype chain, the rest stays
74 // empty.
75 for (var i = 0; i < 5; i++) {
76 object_elements_proto_5_10.__proto__.__proto__.__proto__["proto" + i] = true;
77 }
78
79 var TestObjects = {
80 object_empty: object_empty,
81 array_empty: array_empty,
82 array_int_100: array_int_100,
83 array_int_holey_100: array_int_holey_100,
84 array_int_100_proto_elements: array_int_100_proto_elements,
85 empty_proto_5_10: empty_proto_5_10,
86 empty_proto_5_5_slow: empty_proto_5_5_slow,
87 object_elements_proto_5_10: object_elements_proto_5_10
88 }
89
90 var TestArrays =
91 {
92 array_empty: array_empty,
93 array_int_100: array_int_100,
94 array_int_holey_100: array_int_holey_100,
95 array_int_100_proto_elements: array_int_100_proto_elements
96 }
97
98 // ============================================================================
99
100 function CreateTestFunctionGen(fn) {
101 // Force a new function for each test-object to avoid side-effects due to ICs.
102 return (object) => {
103 var random_comment = "\n// random comment" + Math.random() + "\n";
104 return eval(random_comment + fn.toString());
105 }
106 }
107
108 var TestFunctions = {
109 "Object.keys()": CreateTestFunctionGen(() => {return Object.keys(object)}),
110 "for (in)": CreateTestFunctionGen(() => {
111 var count = 0;
112 var result;
113 for (var key in object) {
114 count++;
115 result = object[key];
116 };
117 return [result, count];
118 }),
119 "for (in) hasOwnProperty()": CreateTestFunctionGen(() => {
120 var count = 0;
121 var result;
122 for (var key in object) {
123 if (!object.hasOwnProperty(key)) continue;
124 count++;
125 result = object[key];
126 };
127 return [result, count];
128 }),
129 "for (i < Object.keys().length)": CreateTestFunctionGen(() => {
130 var count = 0;
131 var result;
132 var keys = Object.keys(object) for (var i = 0; i < keys.length; i++) {
133 count++;
134 result = object[keys[i]];
135 };
136 return [result, count];
137 }),
138 "Object.keys().forEach()": CreateTestFunctionGen(() => {
139 var count = 0;
140 var result;
141 Object.keys(object).forEach((value, index, obj) => {
142 count++;
143 result = value;
144 });
145 return [result, count];
146 }),
147 }
148
149 var TestFunctionsArrays = {
150 "for (i < array.length)": CreateTestFunctionGen(() => {
151 var count = 0;
152 var result;
153 for (var i = 0; i < object.length; i++) {
154 count++;
155 result = object[i];
156 };
157 return [result, count];
158 }),
159 "for (i < length)": CreateTestFunctionGen(() => {
160 var count = 0;
161 var result;
162 var length = object.length;
163 for (var i = 0; i < length; i++) {
164 count++;
165 result = object[i];
166 };
167 return [result, count];
168 })
169 }
170
171 // ============================================================================
172
173 var Benchmarks = [];
174
175 function NewBenchmark(
176 test_function_gen, test_function_name, test_object, test_object_name) {
177 var object = test_object;
178 var name = test_function_name + " " + test_object_name;
179 var test_function = test_function_gen(object);
180 return new Benchmark(name, false, false, 0, test_function)
181 }
182
183 for (var test_function_name in TestFunctions) {
184 var test_function_gen = TestFunctions[test_function_name];
185 var benchmarks = [];
186 for (var test_object_name in TestObjects) {
187 var test_object = TestObjects[test_object_name];
188 var benchmark = NewBenchmark(
189 test_function_gen, test_function_name, test_object, test_object_name);
190 benchmarks.push(benchmark);
191 }
192 Benchmarks.push(new BenchmarkSuite(test_function_name, [1000], benchmarks));
193 }
194
195 for (var test_function_name in TestFunctionsArrays) {
196 var test_function_gen = TestFunctionsArrays[test_function_name];
197 var benchmarks = [];
198 for (var test_array_name in TestArrays) {
199 var test_array = TestArrays[test_array_name];
200 var benchmark = NewBenchmark(
201 test_function_gen, test_function_name, test_array, test_array_name);
202 benchmarks.push(benchmark);
203 }
204 Benchmarks.push(new BenchmarkSuite(test_function_name, [1000], benchmarks));
205 }
206
207 // ============================================================================
OLDNEW
« no previous file with comments | « test/js-perf-test/JSTests.json ('k') | test/js-perf-test/Keys/run.js » ('j') | no next file with comments »

Powered by Google App Engine