This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
129 lines (113 loc) · 3.6 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This is simply the code I used to do some testing of different ways to intersect */
require('./index.js');
var runs = 100000;
var res1 = null;
var testArray1 = ['testVal1', 'testVal2', 'testVal3', 'testVal4', 'testVal5', 'testVal6'];
var testArray2 = ['valTest1', 'valTest2', 'valTest3', 'valTest4', 'testVal5', 'valTest5', 'valTest6', 'valTest7'];
console.time('getIntersect');
for(var i1=0 ; i1 < runs ; i1++) {
res1 = getIntersect(testArray1, testArray2);
}
console.timeEnd('getIntersect');
console.log(res1);
console.log(testArray1);
console.log(testArray2);
var res2 = null;
var testArray1 = ['testVal1', 'testVal2', 'testVal3', 'testVal4', 'testVal5', 'testVal6'];
var testArray2 = ['valTest1', 'valTest2', 'valTest3', 'valTest4', 'testVal5', 'valTest5', 'valTest6', 'valTest7'];
console.time('intersectSafe');
for(var i2=0 ; i2 < runs ; i2++) {
res2 = intersectSafe(testArray1, testArray2);
}
console.timeEnd('intersectSafe');
console.log(res2);
console.log(testArray1);
console.log(testArray2);
var res3 = null;
var testArray1 = ['testVal1', 'testVal2', 'testVal3', 'testVal4', 'testVal5', 'testVal6'];
var testArray2 = ['valTest1', 'valTest2', 'valTest3', 'valTest4', 'testVal5', 'valTest5', 'valTest6', 'valTest7'];
console.time('intersectDestructive');
for(var i3=0 ; i3 < runs ; i3++) {
res3 = intersectDestructive(testArray1, testArray2);
}
console.timeEnd('intersectDestructive');
console.log(res3);
console.log(testArray1);
console.log(testArray2);
var res4 = null;
var testArray1 = ['testVal1', 'testVal2', 'testVal3', 'testVal4', 'testVal5', 'testVal6'];
var testArray2 = ['valTest1', 'valTest2', 'valTest3', 'valTest4', 'testVal5', 'valTest5', 'valTest6', 'valTest7'];
console.time('intersect');
for(var i4=0 ; i4 < runs ; i4++) {
res4 = testArray1.intersect(testArray2);
}
console.timeEnd('intersect');
console.log(res4);
console.log(testArray1);
console.log(testArray2);
function getIntersect(arr1, arr2) {
var r = [], o = {}, l = arr2.length, i, v;
for (i = 0; i < l; i++) {
o[arr2[i]] = true;
}
l = arr1.length;
for (i = 0; i < l; i++) {
v = arr1[i];
if (v in o) {
r.push(v);
}
}
return r;
}
/**
* Credits for this should go to Edgar Villegas Alvarado
* He showed a fast intersect solution on Stack Overflow:
* http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript
*
* This is an expansion on his code, changed a bit to make it more versatile and "safe".
* The additions make sure the arrays are sorted, but makes the code slower.
* In return the original arrays doesn't get changed (sorted).
*/
function intersectSafe(arr1, arr2) {
var ai = 0, bi = 0, result = new Array(), a = arr1.slice(0).sort(), b = arr2.slice(0).sort();
while(ai < a.length && bi < b.length) {
if(a[ai] < b[bi]) {
ai++;
} else if(a[ai] > b[bi]) {
bi++;
} else {
result.push(a[ai]);
ai++; bi++;
}
}
return result;
}
/* destructively finds the intersection of
* two arrays in a simple fashion.
*
* PARAMS
* a - first array, must already be sorted
* b - second array, must already be sorted
*
* NOTES
* State of input arrays is undefined when
* the function returns. They should be
* (prolly) be dumped.
*
* Should have O(n) operations, where n is
* n = MIN(a.length, b.length)
*/
function intersectDestructive(a, b) {
a.sort(); b.sort();
var result = new Array();
while( a.length > 0 && b.length > 0 ) {
if (a[0] < b[0] ) { a.shift(); }
else if (a[0] > b[0] ) { b.shift(); }
else {
/* they're equal */
result.push(a.shift());
b.shift();
}
}
return result;
}