From d702e00446bdd3ca7336be65b8f0edd92c48589f Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 30 Apr 2012 22:32:07 -0400 Subject: [PATCH] lodash: Optimize `intersection`. [jddalton] Former-commit-id: f15eb7429ab4f14a4b096f5ba72f3662f9ed23d7 --- lodash.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lodash.js b/lodash.js index cd9094993..a626a0ba7 100644 --- a/lodash.js +++ b/lodash.js @@ -1101,12 +1101,20 @@ * // => [1, 2] */ function intersection(array) { - var rest = slice.call(arguments, 1); - return filter(uniq(array), function(value) { - return every(rest, function(other) { - return indexOf(other, value) > -1; - }); - }); + var value, + index = -1, + length = array.length, + others = slice.call(arguments, 1), + result = []; + + while (++index < length) { + value = array[index]; + if (indexOf(result, value) < 0 && + every(others, function(other) { return indexOf(other, value) > -1; })) { + result.push(value); + } + } + return result; } /**