Reviewers: Mads Ager, Description: Speed up join on arrays.
Please review this at http://codereview.chromium.org/457021 SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/ Affected files: M src/array.js Index: src/array.js =================================================================== --- src/array.js (revision 3389) +++ src/array.js (working copy) @@ -77,7 +77,8 @@ var key = keys[i]; if (key != last_key) { var e = array[key]; - builder.add(convert(e)); + if (typeof(e) !== 'string') e = convert(e); + builder.add(e); last_key = key; } } @@ -114,18 +115,37 @@ if (length == 1) { var e = array[0]; if (!IS_UNDEFINED(e) || (0 in array)) { + if (typeof(e) === 'string') return e; return convert(e); } } var builder = new StringBuilder(); - for (var i = 0; i < length; i++) { - var e = array[i]; - if (i != 0) builder.add(separator); - if (!IS_UNDEFINED(e) || (i in array)) { - builder.add(convert(e)); + // We pull the empty separator check outside the loop for speed! + if (separator.length == 0) { + for (var i = 0; i < length; i++) { + var e = array[i]; + if (!IS_UNDEFINED(e) || (i in array)) { + if (typeof(e) !== 'string') e = convert(e); + if (e.length > 0) { + var elements = builder.elements; + elements[elements.length] = e; + } + } } + } else { + for (var i = 0; i < length; i++) { + var e = array[i]; + if (i != 0) builder.add(separator); + if (!IS_UNDEFINED(e) || (i in array)) { + if (typeof(e) !== 'string') e = convert(e); + if (e.length > 0) { + var elements = builder.elements; + elements[elements.length] = e; + } + } + } } return builder.generate(); } finally { @@ -136,12 +156,14 @@ function ConvertToString(e) { + if (typeof(e) === 'string') return e; if (e == null) return ''; else return ToString(e); } function ConvertToLocaleString(e) { + if (typeof(e) === 'string') return e; if (e == null) return ''; else { // e_obj's toLocaleString might be overwritten, check if it is a function. -- v8-dev mailing list v8-dev@googlegroups.com http://groups.google.com/group/v8-dev