Time comparation of Intersection Methods
JSLitmus Test
HTML
<script src="http://www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<h1>Time comparation of Intersection Methods</h1>
<a href="http://www.broofa.com/Tools/JSLitmus/">banchmark url reference</a>
CSS
body{padding:1em;font-family:tahoma}
h1{font-weight:bold}
a{line-height:3em; font-style:italic}
JavaScript
mainList = [];
toSearchList = [];
mainListd1 = [];
toSearchListd1 = [];
mainListd2 = [];
toSearchListd2 = [];
for(var i = 0; i < 1000; i++) {
var randomnumber=Math.floor(Math.random()*100)+1;
var randomnumber2=Math.floor(Math.random()*100)+1;
mainList.push(randomnumber);
toSearchList.push(randomnumber2);
mainListd1.push(randomnumber);
toSearchListd1.push(randomnumber2);
mainListd2.push(randomnumber);
toSearchListd2.push(randomnumber2);
}
mainListd1.sort();
toSearchListd1.sort();
mainListd2.sort();
toSearchListd2.sort();
function SimpleJsLoops(x, y){
var ret = [];
for (var i = 0; i < x.length; i++) {
for (var z = 0; z < y.length; z++) {
if (x[i] == y[z]) {
ret.push(i);
break;
}
}
}
return ret;
}
function intersection(x, y) {
x.sort();
y.sort();
var i = j = 0;
var ret = [];
while (i < x.length && j < y.length) {
if (x[i] < y[j]) i++;
else if (y[j] < x[i]) j++;
else {
ret.push(i);
i++, j++;
}
}
return ret;
}
function intersect_safe(a, b)
{
var ai = bi= 0;
var result = [];
while( ai < a.length && bi < b.length ){
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else /* they're equal */
{
result.push(ai);
ai++;
bi++;
}
}
return result;
}
function intersection_destructive(a, b)
{
var result = [];
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;
}
var arrayContains = Array.prototype.indexOf ?
function(arr, val) {
return arr.indexOf(val) > -1;
} :
function(arr, val) {
var i = arr.length;
while...