# Glide apps + vectors

**URL:** <https://community.glideapps.com/t/glide-apps-vectors/68332>\
**Category:** Ask for Help\
**Created:** [December 4, 2023, 2:35am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332 "2023-12-04T02:35:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Airbid](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/airbid/32/60953_2.png) [@Airbid](https://community.glideapps.com/u/Airbid)\
**Post date:** [December 4, 2023, 2:35am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/1 "2023-12-04T02:35:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [December 4, 2023, 2:58am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/2 "2023-12-04T02:58:23Z")

</div>

Can you elaborate about how the calculations are performed?  
Are you using Glide Native Tables, or some other external data source?

---

<div class="post-metadata">

**Author:** ![Airbid](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/airbid/32/60953_2.png) [@Airbid](https://community.glideapps.com/u/Airbid)\
**Post date:** [December 4, 2023, 3:09am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/3 "2023-12-04T03:09:13Z")

</div>

glide native tables - using the javascript plugin

```auto

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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [December 4, 2023, 3:15am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/4 "2023-12-04T03:15:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [December 4, 2023, 3:21am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/5 "2023-12-04T03:21:12Z")

</div>

Do you see speed differences between different devices, or is it slower on the same computer?

---

<div class="post-metadata">

**Author:** ![Airbid](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/airbid/32/60953_2.png) [@Airbid](https://community.glideapps.com/u/Airbid)\
**Post date:** [December 4, 2023, 4:14am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/6 "2023-12-04T04:14:11Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [December 4, 2023, 4:33am UTC](https://community.glideapps.com/t/glide-apps-vectors/68332/7 "2023-12-04T04:33:11Z")

</div>

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.
