Last active
November 27, 2016 10:53
-
-
Save cevek/bf063a468dd0295ea4e05954e29b8ba4 to your computer and use it in GitHub Desktop.
BinarySearch vs ArrayFullSearch vs HashSearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//results for 10 items array/hash | |
// 3,6,8,10ms/1m - binary search | |
// 15ms/1m - array fullscan | |
// 10ms/1m - hash fullscan | |
// 2ms/1m - hash found | |
function abc(fn, title, count, arg, arg2) { | |
console.time(title); | |
var result; | |
var i = count; | |
while(i--) { | |
result = fn(arg, arg2); | |
} | |
console.timeEnd(title); | |
return result; | |
} | |
function Atom(id) { | |
this.id = id; | |
} | |
const atoms = []; | |
for (let i = 0; i < 1000; i++) { | |
atoms[i] = new Atom(i + 1); | |
} | |
const uniqueAtom = new Atom(10000); | |
const uniqueAtom2 = new Atom(-10); | |
function createHash(size) { | |
const items = []; | |
const itemsHash = {}; | |
for (let i = 0; i < size; i++) { | |
const atom = atoms[i]; | |
items[i] = atom; | |
itemsHash[atom.id] = atom; | |
} | |
return itemsHash; | |
} | |
function createOnlyArray(size) { | |
const items = []; | |
for (let i = 0; i < size; i++) { | |
const atom = atoms[i]; | |
items[i] = atom; | |
} | |
return items; | |
} | |
function searchInArray(items, val) { | |
let i = items.length; | |
while (i-- && items[i] !== val); | |
return i !== -1; | |
} | |
function binaryIndexOf(array, searchElement) { | |
let minIndex = 0; | |
let maxIndex = array.length - 1; | |
let currentIndex = 0; | |
let currentElement; | |
const search = searchElement.id; | |
while (minIndex <= maxIndex) { | |
currentIndex = (minIndex + maxIndex) >> 1; | |
currentElement = array[currentIndex].id; | |
if (currentElement < search) { | |
minIndex = currentIndex + 1; | |
} | |
else if (currentElement > search) { | |
maxIndex = currentIndex - 1; | |
} | |
else { | |
return currentIndex; | |
} | |
} | |
return -1; | |
} | |
function binaryTests(fn) { | |
console.assert(fn([0,1,2], 0) == 0); | |
console.assert(fn([0,1,2], 1) == 1); | |
console.assert(fn([0,1,2], 2) == 2); | |
console.assert(fn([0,1,2], 3) == -4); | |
console.assert(fn([0,1,2], -1) == -1); | |
console.assert(fn([0,1,20], 2) == -3); | |
console.assert(fn([0,1,2,3], 0) == 0); | |
console.assert(fn([0,1,2,3], 1) == 1); | |
console.assert(fn([0,1,2,3], 2) == 2); | |
console.assert(fn([0,1,2,3], 3) == 3); | |
console.assert(fn([0,1,2,3], 4) == -5); | |
console.assert(fn([0,1,2,3], -1) == -1); | |
console.assert(fn([0,1,2,3], -10) == -1); | |
console.assert(fn([0,1,20,30], 2) == -3); | |
console.assert(fn([0,1,20,30], 20) == 2); | |
console.assert(fn([0,1,20,30], 30) == 3); | |
console.assert(fn([0,1,20,30], 4) == -3); | |
console.assert(fn([-1,0,1,20,30], -1) == 0); | |
console.assert(fn([-1,0,1,20,30], -2) == -1); | |
console.assert(fn([1,2,3], 1) == 0); | |
} | |
//binaryTests(binaryIndexOf); | |
function searchInHash(itemsHash, val) { | |
return itemsHash[val.id] === val; | |
} | |
const hashes10 = createHash(10); | |
const items10 = createOnlyArray(10); | |
const hashes20 = createHash(20); | |
const items20 = createOnlyArray(20); | |
const hashes100 = createHash(100); | |
const items100 = createOnlyArray(100); | |
const hashes1000 = createHash(1000); | |
const items1000 = createOnlyArray(1000); | |
/***************** create *****************/ | |
// abc(createHash, 'createHash10', 1000000, 10, 0); | |
// abc(createOnlyArray, 'createOnlyArray10', 1000000, 10, 0); | |
/***************** 10 *****************/ | |
// abc(searchInArray, 'searchInArray10U', 10000000, items10, uniqueAtom); | |
// abc(searchInArray, 'searchInArray10M', 10000000, items10, items10[5]); | |
// abc(binaryIndexOf, 'binaryIndexOf10U', 10000000, items10, uniqueAtom); | |
// abc(binaryIndexOf, 'binaryIndexOf10M', 10000000, items10, items10[5]); | |
// abc(searchInHash, 'searchInHash10U', 10000000, hashes10, uniqueAtom); | |
// abc(searchInHash, 'searchInHash10M', 10000000, hashes10, items10[5]); | |
/***************** 20 *****************/ | |
// abc(searchInHash, 'searchInHash20U', 10000000, hashes20, uniqueAtom); | |
// abc(searchInHash, 'searchInHash20M', 10000000, hashes20, items20[10]); | |
// abc(binaryIndexOf, 'binaryIndexOf20U', 10000000, items20, uniqueAtom); | |
// abc(binaryIndexOf, 'binaryIndexOf20M', 10000000, items20, items20[10]); | |
// abc(searchInArray, 'searchInArray20U', 10000000, items20, uniqueAtom); | |
// abc(searchInArray, 'searchInArray20M', 10000000, items20, items20[10]); | |
/***************** 100 *****************/ | |
// abc(binaryIndexOf, 'binaryIndexOf100U', 10000000, items100, uniqueAtom); | |
// abc(binaryIndexOf, 'binaryIndexOf100M', 10000000, items100, items100[50]); | |
// abc(searchInHash, 'searchInHash100U', 10000000, hashes100, uniqueAtom); | |
/***************** 1000 *****************/ | |
// abc(searchInHash, 'searchInHash1000U', 10000000, hashes1000, uniqueAtom); | |
// abc(binaryIndexOf, 'binaryIndexOf1000', 10000000, items1000, uniqueAtom); | |
function abc2() { | |
var result; | |
var i = 10000000; | |
while (i--) { | |
var minIndex = 0; | |
var maxIndex = items10.length - 1; | |
var currentIndex = 0; | |
var currentElement; | |
var search = uniqueAtom.id; | |
while (minIndex <= maxIndex) { | |
currentIndex = (minIndex + maxIndex) >> 1; | |
currentElement = items10[currentIndex].id; | |
if (currentElement < search) { | |
minIndex = currentIndex + 1; | |
} | |
else if (currentElement > search) { | |
maxIndex = currentIndex - 1; | |
} | |
else { | |
result = currentIndex; | |
break; | |
} | |
} | |
} | |
return result; | |
} | |
/* | |
console.time('flow'); | |
abc2(); | |
console.timeEnd('flow'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment