Commit c260e4f7 authored by sandholm@chromium.org's avatar sandholm@chromium.org

Simplify Join and speedup joining arrays of numbers.

Review URL: http://codereview.chromium.org/6173004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6243 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent a1390940
...@@ -117,19 +117,16 @@ function Join(array, length, separator, convert) { ...@@ -117,19 +117,16 @@ function Join(array, length, separator, convert) {
// Fast case for one-element arrays. // Fast case for one-element arrays.
if (length == 1) { if (length == 1) {
var e = array[0]; var e = array[0];
if (!IS_UNDEFINED(e) || (0 in array)) { if (IS_STRING(e)) return e;
if (IS_STRING(e)) return e; return convert(e);
return convert(e);
}
return '';
} }
// Construct an array for the elements. // Construct an array for the elements.
var elements = new $Array(length); var elements = new $Array(length);
var elements_length = 0;
// We pull the empty separator check outside the loop for speed! // We pull the empty separator check outside the loop for speed!
if (separator.length == 0) { if (separator.length == 0) {
var elements_length = 0;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var e = array[i]; var e = array[i];
if (!IS_UNDEFINED(e)) { if (!IS_UNDEFINED(e)) {
...@@ -142,16 +139,25 @@ function Join(array, length, separator, convert) { ...@@ -142,16 +139,25 @@ function Join(array, length, separator, convert) {
if (!IS_UNDEFINED(result)) return result; if (!IS_UNDEFINED(result)) return result;
return %StringBuilderConcat(elements, elements_length, ''); return %StringBuilderConcat(elements, elements_length, '');
} }
// Non-empty separator. // Non-empty separator case.
for (var i = 0; i < length; i++) { // If the first element is a number then use the heuristic that the
var e = array[i]; // remaining elements are also likely to be numbers.
if (!IS_UNDEFINED(e)) { if (!IS_NUMBER(array[0])) {
for (var i = 0; i < length; i++) {
var e = array[i];
if (!IS_STRING(e)) e = convert(e); if (!IS_STRING(e)) e = convert(e);
elements[i] = e; elements[i] = e;
} else {
elements[i] = '';
} }
} } else {
for (var i = 0; i < length; i++) {
var e = array[i];
if (IS_NUMBER(e)) elements[i] = %_NumberToString(e);
else {
if (!IS_STRING(e)) e = convert(e);
elements[i] = e;
}
}
}
var result = %_FastAsciiArrayJoin(elements, separator); var result = %_FastAsciiArrayJoin(elements, separator);
if (!IS_UNDEFINED(result)) return result; if (!IS_UNDEFINED(result)) return result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment