diff --git a/build.js b/build.js index e8715ae5a..0da92cdf3 100755 --- a/build.js +++ b/build.js @@ -814,7 +814,7 @@ // remove IE `shift` and `splice` fix from mutator Array functions mixin source = source.replace(/(?:\s*\/\/.*)*\n( +)if *\(value.length *=== *0[\s\S]+?\n\1}/, ''); - // remove `noCharByIndex` from `_.reduceRight` and `_.toArray` + // remove `noCharByIndex` from `_.reduceRight` source = source.replace(/noCharByIndex *&&[^:]+: *([^;]+)/g, '$1'); source = removeVar(source, 'extendIteratorOptions'); diff --git a/lodash.js b/lodash.js index e361450a2..605e4042a 100644 --- a/lodash.js +++ b/lodash.js @@ -129,12 +129,15 @@ var hasDontEnumBug = !propertyIsEnumerable.call({ 'valueOf': 0 }, 'valueOf'); /** - * Detect support for accessing string characters by index: + * Detect lack of support for accessing string characters by index: * IE < 8 can't access characters by index and IE 8 can only access * characters by index on string literals. */ var noCharByIndex = ('x'[0] + Object('x')[0]) != 'xx'; + /** Detect if `Array#slice` cannot be used to convert strings to arrays (e.g. Opera < 10.52) */ + var noArraySliceOnStrings = slice.call('x')[0] != 'x'; + /* Detect if `Function#bind` exists and is inferred to be fast (i.e. all but V8) */ var isBindFast = nativeBind && /\n|Opera/.test(nativeBind + toString.call(window.opera)); @@ -727,7 +730,7 @@ 'beforeLoop': { 'array': 'if (toString.call(iteratee) == stringClass) return collection.indexOf(target) > -1' }, - 'inLoop': 'if (iteratee[index] === target) return true', + 'inLoop': 'if (iteratee[index] === target) return true' }); /** @@ -1171,7 +1174,7 @@ } var length = collection.length; if (length === length >>> 0) { - return noCharByIndex && toString.call(collection) == stringClass + return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string') ? collection.split('') : slice.call(collection); } diff --git a/perf/perf.js b/perf/perf.js index 770ac878c..20a6bf168 100644 --- a/perf/perf.js +++ b/perf/perf.js @@ -872,6 +872,28 @@ /*--------------------------------------------------------------------------*/ + suites.push( + Benchmark.Suite('`_.toArray` with an array') + .add('Lo-Dash', function() { + lodash.toArray(numbers); + }) + .add('Underscore', function() { + _.toArray(numbers); + }) + ); + + suites.push( + Benchmark.Suite('`_.toArray` with an object') + .add('Lo-Dash', function() { + lodash.toArray(object); + }) + .add('Underscore', function() { + _.toArray(object); + }) + ); + + /*--------------------------------------------------------------------------*/ + suites.push( Benchmark.Suite('`_.union`') .add('Lo-Dash', function() {