From 090fb09955b63cfb186c7a78ce80dc49fff5396c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 6 Dec 2012 23:09:41 -0800 Subject: [PATCH] Optimize `_.union`. Former-commit-id: 0cba8cac81a621b1fdbe8868ab406a30eb1d743f --- lodash.js | 6 ++++-- perf/perf.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index 1664a1296..66a2f605c 100644 --- a/lodash.js +++ b/lodash.js @@ -3140,11 +3140,13 @@ if (isLarge) { // manually coerce `computed` to a string because `hasOwnProperty`, in // some older versions of Firefox, coerces objects incorrectly - seen = hasOwnProperty.call(cache, computed + '') ? cache[computed] : (cache[computed] = []); + var inited = hasOwnProperty.call(cache, computed + '') + ? !(seen = cache[computed]) + : (seen = []); } if (isSorted ? !index || seen[seen.length - 1] !== computed - : indexOf(seen, computed) < 0 + : inited || indexOf(seen, computed) < 0 ) { if (callback || isLarge) { seen.push(computed); diff --git a/perf/perf.js b/perf/perf.js index a8d80a108..a53ea27a9 100644 --- a/perf/perf.js +++ b/perf/perf.js @@ -1631,6 +1631,42 @@ ) ); + suites.push( + Benchmark.Suite('`_.union` iterating an array of 50 elements') + .add(buildName, { + 'fn': 'lodash.union(twentyFiveValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + .add(otherName, { + 'fn': '_.union(twentyFiveValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + ); + + suites.push( + Benchmark.Suite('`_.union` iterating an array of 75 elements') + .add(buildName, { + 'fn': 'lodash.union(fiftyValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + .add(otherName, { + 'fn': '_.union(fiftyValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + ); + + suites.push( + Benchmark.Suite('`_.union` iterating an array of 100 elements') + .add(buildName, { + 'fn': 'lodash.union(seventyFiveValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + .add(otherName, { + 'fn': '_.union(seventyFiveValues, twentyFiveValues2);', + 'teardown': 'function multiArrays(){}' + }) + ); + /*--------------------------------------------------------------------------*/ suites.push(