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

Side by Side Diff: src/array.js

Issue 6173004: Simplify Join and speedup joining arrays of numbers. (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 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 110
111 // Attempt to convert the elements. 111 // Attempt to convert the elements.
112 try { 112 try {
113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) { 113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) {
114 return SparseJoin(array, length, convert); 114 return SparseJoin(array, length, convert);
115 } 115 }
116 116
117 // Fast case for one-element arrays. 117 // Fast case for one-element arrays.
118 if (length == 1) { 118 if (length == 1) {
119 var e = array[0]; 119 var e = array[0];
120 if (!IS_UNDEFINED(e) || (0 in array)) { 120 if (IS_STRING(e)) return e;
121 if (IS_STRING(e)) return e; 121 return convert(e);
122 return convert(e);
123 }
124 return '';
125 } 122 }
126 123
127 // Construct an array for the elements. 124 // Construct an array for the elements.
128 var elements = new $Array(length); 125 var elements = new $Array(length);
129 var elements_length = 0;
130 126
131 // We pull the empty separator check outside the loop for speed! 127 // We pull the empty separator check outside the loop for speed!
132 if (separator.length == 0) { 128 if (separator.length == 0) {
129 var elements_length = 0;
133 for (var i = 0; i < length; i++) { 130 for (var i = 0; i < length; i++) {
134 var e = array[i]; 131 var e = array[i];
135 if (!IS_UNDEFINED(e)) { 132 if (!IS_UNDEFINED(e)) {
136 if (!IS_STRING(e)) e = convert(e); 133 if (!IS_STRING(e)) e = convert(e);
137 elements[elements_length++] = e; 134 elements[elements_length++] = e;
138 } 135 }
139 } 136 }
140 elements.length = elements_length; 137 elements.length = elements_length;
141 var result = %_FastAsciiArrayJoin(elements, ''); 138 var result = %_FastAsciiArrayJoin(elements, '');
142 if (!IS_UNDEFINED(result)) return result; 139 if (!IS_UNDEFINED(result)) return result;
143 return %StringBuilderConcat(elements, elements_length, ''); 140 return %StringBuilderConcat(elements, elements_length, '');
144 } 141 }
145 // Non-empty separator. 142 // Non-empty separator case.
146 for (var i = 0; i < length; i++) { 143 // If the first element is a number then use the heuristic that the
147 var e = array[i]; 144 // remaining elements are also likely to be numbers.
148 if (!IS_UNDEFINED(e)) { 145 if (!IS_NUMBER(array[0])) {
146 for (var i = 0; i < length; i++) {
147 var e = array[i];
149 if (!IS_STRING(e)) e = convert(e); 148 if (!IS_STRING(e)) e = convert(e);
150 elements[i] = e; 149 elements[i] = e;
151 } else {
152 elements[i] = '';
153 } 150 }
154 } 151 } else {
152 for (var i = 0; i < length; i++) {
153 var e = array[i];
154 if (IS_NUMBER(e)) elements[i] = %_NumberToString(e);
155 else {
156 if (!IS_STRING(e)) e = convert(e);
157 elements[i] = e;
158 }
159 }
160 }
155 var result = %_FastAsciiArrayJoin(elements, separator); 161 var result = %_FastAsciiArrayJoin(elements, separator);
156 if (!IS_UNDEFINED(result)) return result; 162 if (!IS_UNDEFINED(result)) return result;
157 163
158 var length2 = (length << 1) - 1; 164 var length2 = (length << 1) - 1;
159 var j = length2; 165 var j = length2;
160 var i = length; 166 var i = length;
161 elements[--j] = elements[--i]; 167 elements[--j] = elements[--i];
162 while (i > 0) { 168 while (i > 0) {
163 elements[--j] = separator; 169 elements[--j] = separator;
164 elements[--j] = elements[--i]; 170 elements[--j] = elements[--i];
(...skipping 1056 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1227 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1222 "reduce", getFunction("reduce", ArrayReduce, 1), 1228 "reduce", getFunction("reduce", ArrayReduce, 1),
1223 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) 1229 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1)
1224 )); 1230 ));
1225 1231
1226 %FinishArrayPrototypeSetup($Array.prototype); 1232 %FinishArrayPrototypeSetup($Array.prototype);
1227 } 1233 }
1228 1234
1229 1235
1230 SetupArray(); 1236 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