File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { bench , group , run } from 'mitata'
2
+ import { tree } from '../lib/core/tree.js'
3
+
4
+ const contentLength = Buffer . from ( 'Content-Length' )
5
+ const contentLengthUpperCase = Buffer . from ( 'Content-Length' . toUpperCase ( ) )
6
+ const contentLengthLowerCase = Buffer . from ( 'Content-Length' . toLowerCase ( ) )
7
+
8
+ group ( 'tree.search' , ( ) => {
9
+ bench ( 'content-length' , ( ) => {
10
+ tree . lookup ( contentLengthLowerCase )
11
+ } )
12
+ bench ( 'CONTENT-LENGTH' , ( ) => {
13
+ tree . lookup ( contentLengthUpperCase )
14
+ } )
15
+ bench ( 'Content-Length' , ( ) => {
16
+ tree . lookup ( contentLength )
17
+ } )
18
+ } )
19
+
20
+ await run ( )
Original file line number Diff line number Diff line change @@ -83,7 +83,10 @@ class TstNode {
83
83
while ( node !== null && index < keylength ) {
84
84
let code = key [ index ]
85
85
// A-Z
86
- if ( code >= 0x41 && code <= 0x5a ) {
86
+ // First check if it is bigger than 0x5a.
87
+ // Lowercase letters have higher char codes than uppercase ones.
88
+ // Also we assume that headers will mostly contain lowercase characters.
89
+ if ( code <= 0x5a && code >= 0x41 ) {
87
90
// Lowercase for uppercase.
88
91
code |= 32
89
92
}
@@ -121,6 +124,7 @@ class TernarySearchTree {
121
124
122
125
/**
123
126
* @param {Uint8Array } key
127
+ * @return {any }
124
128
*/
125
129
lookup ( key ) {
126
130
return this . node ?. search ( key ) ?. value ?? null
Original file line number Diff line number Diff line change @@ -13,6 +13,13 @@ describe('Ternary Search Tree', () => {
13
13
assert . throws ( ( ) => tst . insert ( Buffer . from ( '' ) , '' ) )
14
14
} )
15
15
16
+ test ( 'looking up not inserted key returns null' , ( ) => {
17
+ assert . throws ( ( ) => new TernarySearchTree ( ) . insert ( Buffer . from ( '' ) , '' ) )
18
+ const tst = new TernarySearchTree ( )
19
+ tst . insert ( Buffer . from ( 'a' ) , 'a' )
20
+ assert . strictEqual ( tst . lookup ( Buffer . from ( 'non-existant' ) ) , null )
21
+ } )
22
+
16
23
test ( 'duplicate key' , ( ) => {
17
24
const tst = new TernarySearchTree ( )
18
25
const key = Buffer . from ( 'a' )
You can’t perform that action at this time.
0 commit comments