In my opinion that's, again, very personal opinion, there is no slow programming language that is just algorithms in data structure design. We will see later some benchmarks to prove this point. But, please, I really want this to be a takeaway for this talk. We are at Node conference, right? So, hopefully, this can give us hope.
So, yeah, I want to give you also some examples on how to actually enhance your performances in JavaScript, Node, Deno, whatever. So, when you're writing a full text search engine, at a certain point, you have to deal with boolean queries, right? And, or, etc. And at a certain point, you have to compute the intersections of arrays. So, you have multiple arrays and you have to determine the elements that appears in every single array and return that. This is an example using the MapReduce paradigm. And as a former, and I'm highlighting, former functional programming guy, I used to love this a lot. And yeah, you basically, I see Matteo there, he's face palming and you're the reason why I don't code functionally anymore. I hope you know this. We will get there later. But anyway, using MapReduce, it's very handy, in my opinion, but it's not the most performant way to deal with that kind of algorithms.
So, this is, let's call it version one. We can optimize it, sweeping away all the MapReduce stuff and using plain iteration. And then we have to understand how JavaScript works and how algorithms work. And maybe we can have version two, optimize it even more, and go to version three. So, it's not a matter of parity, it's a matter of algorithms. Here, basically, we are just adding a single line that basically starts the intersection from the smallest array. It's a very simple performance tuning that you can apply on these kinds of algorithms. When you run the benchmarks, and there is the reference for the benchmarks, so you can run them yourselves, you will see that you have like 13% of performance enhancements just by removing the MapReduce stuff and using plain loops, because that's how, at the end of the day, computers tend to work.
I want to give you other examples, but before doing that, as I said, I built a full text search engine, and with this PR alone, we incremented the performances by 50%, five zero, 50% of the overall full text search engine performance is just taking care of how intersections are computed. That's why I wanted to bring this example to the table. But another thing that I'd like you to think of is, please, you have to know your language, your runtime, and how to optimize your code for the runtime you're executing it on. And I can give you a couple of examples of that. First of all, who knows the difference between monomorphism and polymorphism? I'm sure there is more people than that. So basically polymorphism is when you create a function that takes multiple arguments, one to infinite arguments, and you basically call the same function with the same argument types. So if you have an add function where you compute the addition of two numbers, you will always pass numbers to that function. So it's monomorphic.
Comments