Skip to content

Commit bbf8aef

Browse files
committed
faster implementation
1 parent 56fad2e commit bbf8aef

File tree

2 files changed

+33
-63
lines changed

2 files changed

+33
-63
lines changed

index.js

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* arr-diff <https://github.com/jonschlinkert/arr-diff>
33
*
4-
* Copyright (c) 2014 Jon Schlinkert, contributors.
4+
* Copyright (c) 2014-2016, Jon Schlinkert.
55
* Licensed under the MIT License
66
*/
77

@@ -10,49 +10,19 @@
1010
var flatten = require('arr-flatten');
1111
var slice = [].slice;
1212

13-
/**
14-
* Return the difference between the first array and
15-
* additional arrays.
16-
*
17-
* ```js
18-
* var diff = require('{%= name %}');
19-
*
20-
* var a = ['a', 'b', 'c', 'd'];
21-
* var b = ['b', 'c'];
22-
*
23-
* console.log(diff(a, b))
24-
* //=> ['a', 'd']
25-
* ```
26-
*
27-
* @param {Array} `a`
28-
* @param {Array} `b`
29-
* @return {Array}
30-
* @api public
31-
*/
32-
33-
function diff(arr, arrays) {
34-
var argsLen = arguments.length;
35-
var len = arr.length, i = -1;
36-
var res = [], arrays;
37-
38-
if (argsLen === 1) {
39-
return arr;
13+
module.exports = function(arr, arrays) {
14+
arrays = flatten(slice.call(arguments, 1));
15+
var len = arrays.length;
16+
for (var i = 0; i < len; i++) {
17+
remove(arr, arrays[i]);
4018
}
41-
42-
if (argsLen > 2) {
43-
arrays = flatten(slice.call(arguments, 1));
44-
}
45-
46-
while (++i < len) {
47-
if (!~arrays.indexOf(arr[i])) {
48-
res.push(arr[i]);
49-
}
19+
return arr;
20+
};
21+
22+
function remove(arr, ele) {
23+
var idx = arr.indexOf(ele);
24+
while (idx !== -1) {
25+
arr.splice(idx, 1);
26+
idx = arr.indexOf(ele);
5027
}
51-
return res;
5228
}
53-
54-
/**
55-
* Expose `diff`
56-
*/
57-
58-
module.exports = diff;

test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
'use strict';
22

33
require('mocha');
4-
require('should');
4+
var assert = require('assert');
55
var diff = require('./');
66

7-
describe('diff', function () {
8-
it('should diff array:', function () {
9-
diff(['a', 'b', 'c'], ['b', 'c', 'e']).should.eql(['a']);
10-
diff(['x', 'b', 'c', 'e', 'y'], ['b', 'x', 'e']).should.eql(['c', 'y']);
11-
diff(['x', 'x'], ['a', 'b', 'c']).should.eql(['x', 'x']);
12-
diff(['x'], ['a', 'b', 'c']).should.eql(['x']);
7+
describe('diff', function() {
8+
it('should diff array:', function() {
9+
assert.deepEqual(diff(['a', 'b', 'c'], ['b', 'c', 'e']), ['a']);
10+
assert.deepEqual(diff(['x', 'b', 'c', 'e', 'y'], ['b', 'x', 'e']), ['c', 'y']);
11+
assert.deepEqual(diff(['x', 'x'], ['a', 'b', 'c']), ['x', 'x']);
12+
assert.deepEqual(diff(['x'], ['a', 'b', 'c']), ['x']);
1313
});
1414

15-
it('should include duplicates:', function () {
16-
diff(['x', 'b', 'b', 'b', 'c', 'e', 'y'], ['x', 'e']).should.eql(['b', 'b', 'b', 'c', 'y']);
15+
it('should include duplicates:', function() {
16+
assert.deepEqual(diff(['x', 'b', 'b', 'b', 'c', 'e', 'y'], ['x', 'e']), ['b', 'b', 'b', 'c', 'y']);
1717
});
1818

19-
it('should diff elements from multiple arrays:', function () {
20-
diff(['a', 'b', 'c'], ['a'], ['b']).should.eql(['c']);
19+
it('should diff elements from multiple arrays:', function() {
20+
assert.deepEqual(diff(['a', 'b', 'c'], ['a'], ['b']), ['c']);
2121
});
2222

23-
it('should return an empty array if no unique elements are in the first array:', function () {
24-
diff(['a'], ['a', 'b', 'c']).should.eql([]);
23+
it('should return an empty array if no unique elements are in the first array:', function() {
24+
assert.deepEqual(diff(['a'], ['a', 'b', 'c']), []);
2525
});
2626

27-
it('should return the first array if the second array is empty:', function () {
28-
diff(['a', 'b', 'c'], []).should.eql(['a', 'b', 'c']);
27+
it('should return the first array if the second array is empty:', function() {
28+
assert.deepEqual(diff(['a', 'b', 'c'], []), ['a', 'b', 'c']);
2929
});
3030

31-
it('should return the first array if no other args are passed:', function () {
32-
diff(['a', 'b', 'c']).should.eql(['a', 'b', 'c']);
31+
it('should return the first array if no other args are passed:', function() {
32+
assert.deepEqual(diff(['a', 'b', 'c']), ['a', 'b', 'c']);
3333
});
3434

35-
it('should iterate over sparse arguments:', function () {
36-
diff(['a', 'b', 'c'], null, ['a', 'b']).should.eql(['c']);
35+
it('should iterate over sparse arguments:', function() {
36+
assert.deepEqual(diff(['a', 'b', 'c'], null, ['a', 'b']), ['c']);
3737
});
3838
});

0 commit comments

Comments
 (0)