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

Side by Side Diff: src/array.js

Issue 457021: Speed up join on arrays. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years 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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // Optimized for sparse arrays if separator is ''. 70 // Optimized for sparse arrays if separator is ''.
71 function SparseJoin(array, len, convert) { 71 function SparseJoin(array, len, convert) {
72 var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len)); 72 var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len));
73 var builder = new StringBuilder(); 73 var builder = new StringBuilder();
74 var last_key = -1; 74 var last_key = -1;
75 var keys_length = keys.length; 75 var keys_length = keys.length;
76 for (var i = 0; i < keys_length; i++) { 76 for (var i = 0; i < keys_length; i++) {
77 var key = keys[i]; 77 var key = keys[i];
78 if (key != last_key) { 78 if (key != last_key) {
79 var e = array[key]; 79 var e = array[key];
80 builder.add(convert(e)); 80 if (typeof(e) !== 'string') e = convert(e);
81 builder.add(e);
81 last_key = key; 82 last_key = key;
82 } 83 }
83 } 84 }
84 return builder.generate(); 85 return builder.generate();
85 } 86 }
86 87
87 88
88 function UseSparseVariant(object, length, is_array) { 89 function UseSparseVariant(object, length, is_array) {
89 return is_array && 90 return is_array &&
90 length > 1000 && 91 length > 1000 &&
(...skipping 16 matching lines...) Expand all
107 // Attempt to convert the elements. 108 // Attempt to convert the elements.
108 try { 109 try {
109 if (UseSparseVariant(array, length, is_array) && separator === '') { 110 if (UseSparseVariant(array, length, is_array) && separator === '') {
110 return SparseJoin(array, length, convert); 111 return SparseJoin(array, length, convert);
111 } 112 }
112 113
113 // Fast case for one-element arrays. 114 // Fast case for one-element arrays.
114 if (length == 1) { 115 if (length == 1) {
115 var e = array[0]; 116 var e = array[0];
116 if (!IS_UNDEFINED(e) || (0 in array)) { 117 if (!IS_UNDEFINED(e) || (0 in array)) {
118 if (typeof(e) === 'string') return e;
117 return convert(e); 119 return convert(e);
118 } 120 }
119 } 121 }
120 122
121 var builder = new StringBuilder(); 123 var builder = new StringBuilder();
122 124
123 for (var i = 0; i < length; i++) { 125 // We pull the empty separator check outside the loop for speed!
124 var e = array[i]; 126 if (separator.length == 0) {
125 if (i != 0) builder.add(separator); 127 for (var i = 0; i < length; i++) {
126 if (!IS_UNDEFINED(e) || (i in array)) { 128 var e = array[i];
127 builder.add(convert(e)); 129 if (!IS_UNDEFINED(e) || (i in array)) {
130 if (typeof(e) !== 'string') e = convert(e);
131 if (e.length > 0) {
132 var elements = builder.elements;
133 elements[elements.length] = e;
134 }
135 }
136 }
137 } else {
138 for (var i = 0; i < length; i++) {
139 var e = array[i];
140 if (i != 0) builder.add(separator);
141 if (!IS_UNDEFINED(e) || (i in array)) {
142 if (typeof(e) !== 'string') e = convert(e);
143 if (e.length > 0) {
144 var elements = builder.elements;
145 elements[elements.length] = e;
146 }
147 }
128 } 148 }
129 } 149 }
130 return builder.generate(); 150 return builder.generate();
131 } finally { 151 } finally {
132 // Make sure to pop the visited array no matter what happens. 152 // Make sure to pop the visited array no matter what happens.
133 if (is_array) visited_arrays.pop(); 153 if (is_array) visited_arrays.pop();
134 } 154 }
135 } 155 }
136 156
137 157
138 function ConvertToString(e) { 158 function ConvertToString(e) {
159 if (typeof(e) === 'string') return e;
139 if (e == null) return ''; 160 if (e == null) return '';
140 else return ToString(e); 161 else return ToString(e);
141 } 162 }
142 163
143 164
144 function ConvertToLocaleString(e) { 165 function ConvertToLocaleString(e) {
166 if (typeof(e) === 'string') return e;
145 if (e == null) return ''; 167 if (e == null) return '';
146 else { 168 else {
147 // e_obj's toLocaleString might be overwritten, check if it is a function. 169 // e_obj's toLocaleString might be overwritten, check if it is a function.
148 // Call ToString if toLocaleString is not a function. 170 // Call ToString if toLocaleString is not a function.
149 // See issue 877615. 171 // See issue 877615.
150 var e_obj = ToObject(e); 172 var e_obj = ToObject(e);
151 if (IS_FUNCTION(e_obj.toLocaleString)) 173 if (IS_FUNCTION(e_obj.toLocaleString))
152 return e_obj.toLocaleString(); 174 return e_obj.toLocaleString();
153 else 175 else
154 return ToString(e); 176 return ToString(e);
(...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 ArrayIndexOf: 1, 1143 ArrayIndexOf: 1,
1122 ArrayLastIndexOf: 1, 1144 ArrayLastIndexOf: 1,
1123 ArrayPush: 1, 1145 ArrayPush: 1,
1124 ArrayReduce: 1, 1146 ArrayReduce: 1,
1125 ArrayReduceRight: 1 1147 ArrayReduceRight: 1
1126 }); 1148 });
1127 } 1149 }
1128 1150
1129 1151
1130 SetupArray(); 1152 SetupArray();
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