Skip to content

Commit 60853f3

Browse files
committed
optimizations
1 parent 8d22067 commit 60853f3

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

benchmark/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Suite = require('benchmarked');
44
var suite = new Suite({
55
result: false,
66
fixtures: 'fixtures/*.js',
7-
add: 'code/{while-multi*,current,arr*}.js',
7+
add: 'code/{current,arr*}.js',
88
cwd: __dirname
99
});
1010

index.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77

88
'use strict';
99

10+
var flatten = require('arr-flatten');
1011
var slice = require('array-slice');
1112

12-
/**
13-
* Expose `diff`
14-
*/
15-
16-
module.exports = diff;
17-
1813
/**
1914
* Return the difference between the first array and
2015
* additional arrays.
@@ -35,25 +30,29 @@ module.exports = diff;
3530
* @api public
3631
*/
3732

38-
function diff(a, b, c) {
39-
var len = a.length;
40-
var arr = [];
41-
var rest;
33+
function diff(arr, arrays) {
34+
var argsLen = arguments.length;
35+
var len = arr.length, i = -1;
36+
var res = [], arrays;
4237

43-
if (!b) {
44-
return a;
38+
if (argsLen === 1) {
39+
return arr;
4540
}
4641

47-
if (!c) {
48-
rest = b;
49-
} else {
50-
rest = [].concat.apply([], slice(arguments, 1));
42+
if (argsLen > 2) {
43+
arrays = flatten(slice(arguments, 1));
5144
}
5245

53-
while (len--) {
54-
if (rest.indexOf(a[len]) === -1) {
55-
arr.unshift(a[len]);
46+
while (++i < len) {
47+
if (!~arrays.indexOf(arr[i])) {
48+
res.push(arr[i]);
5649
}
5750
}
58-
return arr;
51+
return res;
5952
}
53+
54+
/**
55+
* Expose `diff`
56+
*/
57+
58+
module.exports = diff;

0 commit comments

Comments
 (0)