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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/json.js
===================================================================
--- src/json.js (revision 6268)
+++ src/json.js (working copy)
@@ -184,11 +184,42 @@
}
builder.push("[");
var len = value.length;
- for (var i = 0; i < len; i++) {
- var before = builder.length;
- BasicJSONSerialize(i, value, stack, builder);
- if (before == builder.length) builder.push("null");
- builder.push(",");
+ 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
+ if (IS_STRING(val)) {
+ // First entry is a string. Remaining entries are likely to be strings too.
+ for (var i = 0; i < len; i++) {
+ val = value[i];
+ if (IS_STRING(val)) {
+ builder.push(%QuoteJSONString(val));
+ } else {
+ var before = builder.length;
+ BasicJSONSerialize(i, value, stack, builder);
+ if (before == builder.length) builder.push("null");
+ }
+ builder.push(",");
+ }
+ } else if (IS_NUMBER(val)) {
+ // First entry is a number. Remaining entries are likely to be numbers too.
+ for (var i = 0; i < len; i++) {
+ val = value[i];
+ if (IS_NUMBER(val)) {
+ builder.push(NUMBER_IS_FINITE(val)
+ ? %_NumberToString(val)
+ : "null");
+ } else {
+ var before = builder.length;
+ BasicJSONSerialize(i, value, stack, builder);
+ if (before == builder.length) builder.push("null");
+ }
+ builder.push(",");
+ }
+ } else {
+ for (var i = 0; i < len; i++) {
+ var before = builder.length;
+ BasicJSONSerialize(i, value, stack, builder);
+ if (before == builder.length) builder.push("null");
+ builder.push(",");
+ }
}
stack.pop();
if (builder.pop() != ",") {
« 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