I’m using glide apps in a search app to calculate cosine similarity between two vectors. I know vector databases are made for that, but wanted to give it a try in Glide. Does anybody have an explanation why the calculations are really quick in editor, but much slower on the published app?
Can you elaborate about how the calculations are performed?
Are you using Glide Native Tables, or some other external data source?
glide native tables - using the javascript plugin
function cosineSimilarity() {
// Remove square brackets, split the strings, and convert them to arrays of numbers
let arrayP1 = p1.substring(1, p1.length - 1).split(',').map(Number);
let arrayP2 = p2.substring(1, p2.length - 1).split(',').map(Number);
let dotProduct = 0.0;
let normA = 0.0;
let normB = 0.0;
for (let i = 0; i < arrayP1.length; i++) {
dotProduct += arrayP1[i] * arrayP2[i];
normA += arrayP1[i] * arrayP1[i];
normB += arrayP2[i] * arrayP2[i];
}
// Calculate the cosine similarity
let cosineSimilarity = dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
// Check for division by zero or invalid numbers
if (isNaN(cosineSimilarity) || !isFinite(cosineSimilarity)) {
return "Error: Division by zero or invalid number";
}
// Return the similarity only if it's greater than 0.81
if (cosineSimilarity > 0.81) {
return cosineSimilarity;
} else {
return 0;
}
}
You are not passing parameters to your function!
Take out the first line and the last line…
You can add if-else to check if your p parameters are not empty
Do you see speed differences between different devices, or is it slower on the same computer?
@Jeff_Hager thats the thing, on the same computer it loads much slower on the app. And yes on a different device it’s also slower. But on the editor the vector calculation is working amazingly good on 6,000 vectors at the same time making a real time search useable.
Do you perform this search on multiple rows, thus the javascript is running multiple times? The only thought off the top of my head is that if this is being performed multiple times, then the number of rows may be a factor. Glide typically only runs computed columns as needed, so it won’t necessarily run all code on all rows in one shot in some cases. I guess another possibility is that the editor has more rows and computations preloaded so it feels faster, but I’m not sure. I was initially thinking that the computational power of the device might be a factor, but if you are seeing slowness on the same computer, then I’m not sure what’s going on.
Would you be able to provide a short video demonstrating how your app works? Also show what the p1 and p2 parameter data looks like and how those values are populated.