forked from ashvardanian/SimSIMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
51 lines (44 loc) · 1.46 KB
/
bench.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
const benchmark = require('benchmark');
// Assuming the vectors are of the same length
function cosineDistance(a, b) {
let dotProduct = 0;
let magA = 0;
let magB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
magA += a[i] * a[i];
magB += b[i] * b[i];
}
return 1 - (dotProduct / (Math.sqrt(magA) * Math.sqrt(magB)));
}
// Generate random data for testing
const dimensions = 1536; // Adjust dimensions as needed
const array1 = Array.from({ length: dimensions }, () => Math.random() * 100);
const array2 = Array.from({ length: dimensions }, () => Math.random() * 100);
const floatArray1 = new Float32Array(array1);
const floatArray2 = new Float32Array(array2);
// Create benchmark suite
const singleSuite = new benchmark.Suite('Single Vector Processing');
// Single-vector processing benchmarks
singleSuite
// Pure JavaScript
.add('Array of Numbers', () => {
cosineDistance(array1, array2);
})
.add('TypedArray of Float32', () => {
cosineDistance(floatArray1, floatArray2);
})
.on('cycle', (event) => {
if (event.target.error) {
console.error(String(event.target.error));
} else {
console.log(String(event.target));
}
})
.on('complete', () => {
console.log('Fastest Single-Vector Processing is ' + singleSuite.filter('fastest').map('name'));
})
.run({
noCache: true,
async: false,
});