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

Side by Side Diff: src/json.js

Issue 6164004: Optimize JSON stringify for arrays. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 return void 0; 177 return void 0;
178 } 178 }
179 179
180 180
181 function BasicSerializeArray(value, stack, builder) { 181 function BasicSerializeArray(value, stack, builder) {
182 if (!%PushIfAbsent(stack, value)) { 182 if (!%PushIfAbsent(stack, value)) {
183 throw MakeTypeError('circular_structure', []); 183 throw MakeTypeError('circular_structure', []);
184 } 184 }
185 builder.push("["); 185 builder.push("[");
186 var len = value.length; 186 var len = value.length;
187 for (var i = 0; i < len; i++) { 187 var val = value[0];
Erik Corry 2011/01/12 12:44:41 what if len is 0 and value has an accessor on "0"?
sandholm 2011/01/12 13:36:20 Good point. Fix uploaded. Could you have another l
188 var before = builder.length; 188 if (IS_STRING(val)) {
189 BasicJSONSerialize(i, value, stack, builder); 189 // First entry is a string. Remaining entries are likely to be strings too.
190 if (before == builder.length) builder.push("null"); 190 for (var i = 0; i < len; i++) {
191 builder.push(","); 191 val = value[i];
192 if (IS_STRING(val)) {
193 builder.push(%QuoteJSONString(val));
194 } else {
195 var before = builder.length;
196 BasicJSONSerialize(i, value, stack, builder);
197 if (before == builder.length) builder.push("null");
198 }
199 builder.push(",");
200 }
201 } else if (IS_NUMBER(val)) {
202 // First entry is a number. Remaining entries are likely to be numbers too.
203 for (var i = 0; i < len; i++) {
204 val = value[i];
205 if (IS_NUMBER(val)) {
206 builder.push(NUMBER_IS_FINITE(val)
207 ? %_NumberToString(val)
208 : "null");
209 } else {
210 var before = builder.length;
211 BasicJSONSerialize(i, value, stack, builder);
212 if (before == builder.length) builder.push("null");
213 }
214 builder.push(",");
215 }
216 } else {
217 for (var i = 0; i < len; i++) {
218 var before = builder.length;
219 BasicJSONSerialize(i, value, stack, builder);
220 if (before == builder.length) builder.push("null");
221 builder.push(",");
222 }
192 } 223 }
193 stack.pop(); 224 stack.pop();
194 if (builder.pop() != ",") { 225 if (builder.pop() != ",") {
195 builder.push("[]"); // Zero length array. Push "[" back on. 226 builder.push("[]"); // Zero length array. Push "[" back on.
196 } else { 227 } else {
197 builder.push("]"); 228 builder.push("]");
198 } 229 }
199 230
200 } 231 }
201 232
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 328 }
298 329
299 function SetupJSON() { 330 function SetupJSON() {
300 InstallFunctions($JSON, DONT_ENUM, $Array( 331 InstallFunctions($JSON, DONT_ENUM, $Array(
301 "parse", JSONParse, 332 "parse", JSONParse,
302 "stringify", JSONStringify 333 "stringify", JSONStringify
303 )); 334 ));
304 } 335 }
305 336
306 SetupJSON(); 337 SetupJSON();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698