Last active
April 3, 2021 12:45
-
-
Save qwertyforce/025b859707f869d09a42826f5b4c1723 to your computer and use it in GitHub Desktop.
This file contains 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
const VPTreeFactory = require("vptree"); | |
const crypto = require("crypto"); | |
function hamming_distance(str1, str2) { | |
let distance = 0; | |
for (let i = 0; i < str1.length; i += 1) { | |
if (str1[i] !== str2[i]) { | |
distance += 1; | |
} | |
} | |
return distance; | |
} | |
const phashes=[] | |
for (let i=0;i<500000;i++){ | |
let id = crypto.randomBytes(64).toString('hex'); | |
phashes.push(id) | |
} | |
console.log(`Array length: ${phashes.length}`) | |
console.time("vp-tree build") | |
const vptree=VPTreeFactory.build(phashes, hamming_distance) | |
console.timeEnd("vp-tree build") | |
console.time("vp-tree") | |
console.log(vptree.search("91189ee70f8a857dc8d66e2fc7059cd7c36c1dc320559b0970de0fe1321d65e6",10)) | |
console.timeEnd("vp-tree") | |
console.time("for loop") | |
let qwe=[] | |
for (const phash of phashes){ | |
qwe.push({dist:hamming_distance("91189ee70f8a857dc8d66e2fc7059cd7c36c1dc320559b0970de0fe1321d65e6",phash),phash:phash}) | |
} | |
qwe.sort((a,b)=>a.dist-b.dist) | |
qwe=qwe.slice(0,10) | |
console.timeEnd("for loop") | |
console.time("for loop w/o sorting") | |
qwe=[] | |
for (const phash of phashes){ | |
qwe.push({dist:hamming_distance("91189ee70f8a857dc8d66e2fc7059cd7c36c1dc320559b0970de0fe1321d65e6",phash),phash:phash}) | |
} | |
console.timeEnd("for loop w/o sorting") | |
// console.log(qwe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment