Skip to content

Commit

Permalink
bench: new files
Browse files Browse the repository at this point in the history
  • Loading branch information
CanadaHonk committed Oct 22, 2023
1 parent 946403f commit 151f80e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bench/interp-dispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// taken mostly from hermes

function bench (lc, fc) {
var n, fact;
var res = 0;
while (--lc >= 0) {
n = fc;
fact = n;
while (--n > 1)
fact *= n;
res += fact;
}
return res;
}

let t1 = performance.now();
var res = bench(4e6, 100);
print(performance.now() - t1);
41 changes: 41 additions & 0 deletions bench/pearson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const start = performance.now();

// Define your two arrays of numbers
const array1 = [1, 2, 3, 4, 5];
const array2 = [5, 4, 3, 2, 1];

const mean = arr => {
arr ??= [];

let sum = 0;
for (const x of arr) {
sum += x;
}

return sum / arr.length;
};

// Calculate the mean of each array
// const mean1 = array1.reduce((acc, val) => acc + val, 0) / array1.length;
// const mean2 = array2.reduce((acc, val) => acc + val, 0) / array2.length;
const mean1 = mean(array1);
const mean2 = mean(array2);

// Calculate the numerator and denominators for Pearson correlation
let numerator = 0;
let denominator1 = 0;
let denominator2 = 0;

for (let i = 0; i < array1.length; i++) {
const diff1 = array1[i] - mean1;
const diff2 = array2[i] - mean2;
numerator += diff1 * diff2;
denominator1 += diff1 * diff1;
denominator2 += diff2 * diff2;
}

// Calculate the correlation coefficient
const correlationCoefficient = numerator / Math.sqrt(denominator1 * denominator2);

console.log(correlationCoefficient);
console.log(performance.now() - start);
12 changes: 12 additions & 0 deletions bench/wellformed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let t = performance.now();

let i = 0;
while (i < 100_000) {
'abc'.isWellFormed() // true
'🔥'.isWellFormed() // true
'\uD83D'.isWellFormed() // false

i += 1;
}

console.log(performance.now() - t);

0 comments on commit 151f80e

Please sign in to comment.