Live data from Hacker News

How to Implement a Cosine Similarity Function in TypeScript

alexop.dev

11–20 of 43 posts

Re: How to Implement a Cosine Similarity Function in TypeScript

#12
Great post, but what struck me (again, like every time I look at cos similiarity) is how unreasonably well it works. It's just one of those things that's so "weird" about our world: why would cosine similarity work in n-dimensional semantic spaces? It's so stupid simple, and it intuitively makes sense, and it works really well. Crazy cool.

I'm reminded of that old Eugene Wigner quote: "The most incomprehensible thing about the universe is that it is comprehensible."

Re: How to Implement a Cosine Similarity Function in TypeScript

#13

It’s a nice post, but “using array methods” probably shouldn’t be placed in the “Efficient Implementation” section. As often happens with high-level languages, a single plain old loop is faster than three array methods. Similarly, if you plan to query those vectors in search, you should consider continuous `TypedArray` types and smaller scalars than the double precision `number`. I know very little about JS, but some…

ooh, fantastic.. thankyou !

Im doing a bunch of 3D rotation transforms on millions of points in javascript .. this looks like what I need.

Re: How to Implement a Cosine Similarity Function in TypeScript

#14
post #4

Earlier quoted context omitted.

Thank you for the good feedback. I tried to improve that. I was writing the blog post for myself to understand Cosine Similarity, which is why it's maybe a bit repetitive, but this is the best way for me to learn something. I get your point. Next time I will write it better. Good feedback - I love that.

Ha, when you put it that way, I can totally see why it read like that! It looks super great now. What you have here leaves an entirely different impression, and a stylish one! Two last suggestions: * Now I'm thinking the Why Cosine Similarity Matters for Modern Web Development section belongs at the top, right after your intro. * The angle indicator is still a bit wonky in the diagram. I might even take direction onl…

I think the web animation is really useful for people new to the concept.

Re: How to Implement a Cosine Similarity Function in TypeScript

#19
post #18

I do all my work in Rust now via o3-mini-high and convert to WASM... JS just for DOM and event handling. What is the point of building these CPU-bound functions in TS. Why not Rust->WASM?

Because op knows TS and doesn't know Rust?

What I'm asking really is what is the benefit of TS if we could use decent and improving coding AIs to help us code in Rust, even if we are new to it, or never used it, and compile to WASM. Much faster execution per my experience. I mean not even comparable.

Re: How to Implement a Cosine Similarity Function in TypeScript

#20
post #14

Earlier quoted context omitted.

Ha, when you put it that way, I can totally see why it read like that! It looks super great now. What you have here leaves an entirely different impression, and a stylish one! Two last suggestions: * Now I'm thinking the Why Cosine Similarity Matters for Modern Web Development section belongs at the top, right after your intro. * The angle indicator is still a bit wonky in the diagram. I might even take direction onl…

I think the web animation is really useful for people new to the concept.

Oh, certainly, I meant remove the "Direction-only mode" toggle, not the whole animation!
Post reply on HN