Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: test/mjsunit/array-iteration.js

Issue 553413002: Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper f… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Apply same changes to MapForEach and SetForEach Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 // Respect holes. 61 // Respect holes.
62 a = new Array(20); 62 a = new Array(20);
63 var count = 0; 63 var count = 0;
64 a[2] = 2; 64 a[2] = 2;
65 a[15] = 2; 65 a[15] = 2;
66 a[17] = 4; 66 a[17] = 4;
67 var a = a.filter(function(n) { count++; return n == 2; }); 67 var a = a.filter(function(n) { count++; return n == 2; });
68 assertEquals(3, count); 68 assertEquals(3, count);
69 for (var i in a) assertEquals(2, a[i]); 69 for (var i in a) assertEquals(2, a[i]);
70 70
71 // Create a new object in each function call when receiver is a primitive valu e.
72 a = new Array();
arv (Not doing code reviews) 2014/09/12 16:29:17 nit a = [];
73 [1,2].filter(function() { a.push(this) }, "");
arv (Not doing code reviews) 2014/09/12 16:29:17 nit, space after comma
74 assertTrue(a[0] !== a[1]);
75
76 // Do not create a new object in each function call when receiver is a primiti ve value.
wingo 2014/09/15 09:12:21 "when receiver is not a primitive value" here, and
Diego Pino 2014/09/16 10:45:46 Acknowledged.
77 a = new Array();
78 [1,2].filter(function() { a.push(this) }, {});
79 assertFalse(a[0] !== a[1]);
80
71 })(); 81 })();
72 82
73 83
74 // 84 //
75 // Array.prototype.forEach 85 // Array.prototype.forEach
76 // 86 //
77 (function() { 87 (function() {
78 // Simple use. 88 // Simple use.
79 var a = [0,1]; 89 var a = [0,1];
80 var count = 0; 90 var count = 0;
(...skipping 21 matching lines...) Expand all
102 assertEquals(2, count); 112 assertEquals(2, count);
103 assertArrayEquals([1,1,2,2], a); 113 assertArrayEquals([1,1,2,2], a);
104 114
105 // Respect holes. 115 // Respect holes.
106 a = new Array(20); 116 a = new Array(20);
107 count = 0; 117 count = 0;
108 a[15] = 2; 118 a[15] = 2;
109 a.forEach(function(n) { count++; }); 119 a.forEach(function(n) { count++; });
110 assertEquals(1, count); 120 assertEquals(1, count);
111 121
122 // Create a new object in each function call when receiver is a primitive valu e.
123 a = new Array();
124 [1,2].forEach(function() { a.push(this) }, "");
125 assertTrue(a[0] !== a[1]);
126
127 // Do not create a new object in each function call when receiver is a primiti ve value.
128 a = new Array();
129 [1,2].forEach(function() { a.push(this) }, {});
130 assertFalse(a[0] !== a[1]);
131
112 })(); 132 })();
113 133
114 134
115 // 135 //
116 // Array.prototype.every 136 // Array.prototype.every
117 // 137 //
118 (function() { 138 (function() {
119 // Simple use. 139 // Simple use.
120 var a = [0,1]; 140 var a = [0,1];
121 assertFalse(a.every(function(n) { return n == 0 })); 141 assertFalse(a.every(function(n) { return n == 0 }));
(...skipping 20 matching lines...) Expand all
142 assertArrayEquals([1,1,2,2], a); 162 assertArrayEquals([1,1,2,2], a);
143 163
144 // Respect holes. 164 // Respect holes.
145 a = new Array(20); 165 a = new Array(20);
146 var count = 0; 166 var count = 0;
147 a[2] = 2; 167 a[2] = 2;
148 a[15] = 2; 168 a[15] = 2;
149 assertTrue(a.every(function(n) { count++; return n == 2; })); 169 assertTrue(a.every(function(n) { count++; return n == 2; }));
150 assertEquals(2, count); 170 assertEquals(2, count);
151 171
172 // Create a new object in each function call when receiver is a primitive valu e.
173 a = new Array();
174 [1,2].every(function() { a.push(this); return true; }, "");
175 assertTrue(a[0] !== a[1]);
176
177 // Do not create a new object in each function call when receiver is a primiti ve value.
178 a = new Array();
179 [1,2].every(function() { a.push(this); return true; }, {});
180 assertFalse(a[0] !== a[1]);
181
152 })(); 182 })();
153 183
154 // 184 //
155 // Array.prototype.map 185 // Array.prototype.map
156 // 186 //
157 (function() { 187 (function() {
158 var a = [0,1,2,3,4]; 188 var a = [0,1,2,3,4];
159 189
160 // Simple use. 190 // Simple use.
161 var result = [1,2,3,4,5]; 191 var result = [1,2,3,4,5];
(...skipping 17 matching lines...) Expand all
179 result = [1,2,3,4,5]; 209 result = [1,2,3,4,5];
180 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;})); 210 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret urn n + 1;}));
181 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); 211 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);
182 212
183 // Respect holes. 213 // Respect holes.
184 a = new Array(20); 214 a = new Array(20);
185 a[15] = 2; 215 a[15] = 2;
186 a = a.map(function(n) { return 2*n; }); 216 a = a.map(function(n) { return 2*n; });
187 for (var i in a) assertEquals(4, a[i]); 217 for (var i in a) assertEquals(4, a[i]);
188 218
219 // Create a new object in each function call when receiver is a primitive valu e.
220 a = new Array();
221 [1,2].map(function() { a.push(this) }, "");
222 assertTrue(a[0] !== a[1]);
223
224 // Do not create a new object in each function call when receiver is a primiti ve value.
225 a = new Array();
226 [1,2].map(function() { a.push(this) }, {});
227 assertFalse(a[0] !== a[1]);
228
189 })(); 229 })();
190 230
191 // 231 //
192 // Array.prototype.some 232 // Array.prototype.some
193 // 233 //
194 (function() { 234 (function() {
195 var a = [0,1,2,3,4]; 235 var a = [0,1,2,3,4];
196 236
197 // Simple use. 237 // Simple use.
198 assertTrue(a.some(function(n) { return n == 3})); 238 assertTrue(a.some(function(n) { return n == 3}));
(...skipping 18 matching lines...) Expand all
217 257
218 // Respect holes. 258 // Respect holes.
219 a = new Array(20); 259 a = new Array(20);
220 var count = 0; 260 var count = 0;
221 a[2] = 42; 261 a[2] = 42;
222 a[10] = 2; 262 a[10] = 2;
223 a[15] = 42; 263 a[15] = 42;
224 assertTrue(a.some(function(n) { count++; return n == 2; })); 264 assertTrue(a.some(function(n) { count++; return n == 2; }));
225 assertEquals(2, count); 265 assertEquals(2, count);
226 266
267 // Create a new object in each function call when receiver is a primitive valu e.
268 a = new Array();
269 [1,2].some(function() { a.push(this) }, "");
270 assertTrue(a[0] !== a[1]);
271
272 // Do not create a new object in each function call when receiver is a primiti ve value.
273 a = new Array();
274 [1,2].some(function() { a.push(this) }, {});
275 assertFalse(a[0] !== a[1]);
276
227 })(); 277 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698