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) {
// Fast case for one-element arrays.
if (length == 1) {
var e = array[0];
if (!IS_UNDEFINED(e) || (0 in array)) {
if (IS_STRING(e)) return e;
return convert(e);
}
return '';
}
// Construct an array for the elements.
var elements = new $Array(length);
var elements_length = 0;
// We pull the empty separator check outside the loop for speed!
if (separator.length == 0) {
var elements_length = 0;
for (var i = 0; i < length; i++) {
var e = array[i];
if (!IS_UNDEFINED(e)) {
......@@ -142,14 +139,23 @@ function Join(array, length, separator, convert) {
if (!IS_UNDEFINED(result)) return result;
return %StringBuilderConcat(elements, elements_length, '');
}
// Non-empty separator.
// Non-empty separator case.
// If the first element is a number then use the heuristic that the
// remaining elements are also likely to be numbers.
if (!IS_NUMBER(array[0])) {
for (var i = 0; i < length; i++) {
var e = array[i];
if (!IS_UNDEFINED(e)) {
if (!IS_STRING(e)) e = convert(e);
elements[i] = e;
}
} else {
elements[i] = '';
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);
......
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