-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement simple B-Tree for faster string comparisons #5441
Comments
Can you see this? We recently landed it and Set performed a little better, it might already be using something like that under-the-hood: |
I dont think that Set is optimized for this case, as Set also handles Objects and numbers etc.. So Set must have some code to check if the type is the same and stuff like that. I have the feeling that a b-tree will be significantly faster. |
A switch statement could be more efficient as it allows for direct comparison and may be optimized by the JavaScript engine. |
I kind of doubt that switch case is faster. |
Linear search is very often more efficient than Binary search when the number of items is tiny. I would be very surprised if implementing a binary search for this use case would be faster (regardless Big-O notation). |
Why don't we use hashing for this, const methods = new Set(['POST', 'PUT', 'PATCH', 'TRACE', 'SEARCH']);
if (methods.has(method)) {
// ....
} I can make a PR, If this is what we are looking for. IMHO |
I've explored options above for comparison—linear search, tree, if statement, switch statements, and sets. After conducting ~100 million comparisons, the results points to sets and switch statements as the fastest approaches. |
Thanks @dancastillo I was about to do the same. Out of curiosity I benchmarked with
|
Maybe switch, but Sets let us centralize supported methods and they are usually as fast as it gets when used with only one type
This is what we do right now, and like you said, V8 probably creates an efficient hash table anyway Maybe we could switch to a |
Prerequisites
🚀 Feature Proposal
Implement a B-Tree Class
Motivation
Lets see following lines
fastify/lib/handleRequest.js
Line 30 in 8d66924
This is run on every request. It means, that the last condition, here
method === 'LOCK'
is the slowest, as we have to check first 7 other values, before we potentially hit LOCK.We should implement some simple and understandable b-tree, which only returns true if the element is in the tree and false if not.
Maybe there are other places we can use it.
Maybe using a Map is sufficient? But I think a b-tree would be faster.
Example
The text was updated successfully, but these errors were encountered: