@@ -687,8 +687,92 @@ static KEYWORDS: phf::Map<&'static str, TokenType> = phf_map! {
687
687
688
688
/// Convert a keyword string to TokenType
689
689
/// Uses a PHF map for O(1) time complexity with zero runtime overhead
690
+ /// Optimized with fast-path checks for common keywords
690
691
#[ inline( always) ]
691
692
pub fn keyword_to_token_type ( word : & str ) -> Option < TokenType > {
693
+ // Fast path for the most common keywords
694
+ // Check length first as it's a very cheap operation
695
+ match word. len ( ) {
696
+ 2 => {
697
+ // Common 2-letter keywords: if, in, do
698
+ match word {
699
+ "if" => return Some ( TokenType :: If ) ,
700
+ "in" => return Some ( TokenType :: In ) ,
701
+ "do" => return Some ( TokenType :: Do ) ,
702
+ "as" => return Some ( TokenType :: As ) ,
703
+ "of" => return Some ( TokenType :: Of ) ,
704
+ "is" => return Some ( TokenType :: Is ) ,
705
+ _ => { }
706
+ }
707
+ }
708
+ 3 => {
709
+ // Common 3-letter keywords: let, var, for, new, try
710
+ match word {
711
+ "let" => return Some ( TokenType :: Let ) ,
712
+ "var" => return Some ( TokenType :: Var ) ,
713
+ "for" => return Some ( TokenType :: For ) ,
714
+ "new" => return Some ( TokenType :: New ) ,
715
+ "try" => return Some ( TokenType :: Try ) ,
716
+ "get" => return Some ( TokenType :: Get ) ,
717
+ "set" => return Some ( TokenType :: Set ) ,
718
+ _ => { }
719
+ }
720
+ }
721
+ 4 => {
722
+ // Common 4-letter keywords: this, null, true, void, else
723
+ match word {
724
+ "this" => return Some ( TokenType :: This ) ,
725
+ "null" => return Some ( TokenType :: Null ) ,
726
+ "true" => return Some ( TokenType :: True ) ,
727
+ "void" => return Some ( TokenType :: Void ) ,
728
+ "else" => return Some ( TokenType :: Else ) ,
729
+ "from" => return Some ( TokenType :: From ) ,
730
+ "enum" => return Some ( TokenType :: Enum ) ,
731
+ "type" => return Some ( TokenType :: Type ) ,
732
+ _ => { }
733
+ }
734
+ }
735
+ 5 => {
736
+ // Common 5-letter keywords: const, class, super, break, await, yield
737
+ match word {
738
+ "const" => return Some ( TokenType :: Const ) ,
739
+ "class" => return Some ( TokenType :: Class ) ,
740
+ "super" => return Some ( TokenType :: Super ) ,
741
+ "break" => return Some ( TokenType :: Break ) ,
742
+ "await" => return Some ( TokenType :: Await ) ,
743
+ "yield" => return Some ( TokenType :: Yield ) ,
744
+ "async" => return Some ( TokenType :: Async ) ,
745
+ _ => { }
746
+ }
747
+ }
748
+ 6 => {
749
+ // Common 6-letter keywords: return, import, export, switch, static
750
+ match word {
751
+ "return" => return Some ( TokenType :: Return ) ,
752
+ "import" => return Some ( TokenType :: Import ) ,
753
+ "export" => return Some ( TokenType :: Export ) ,
754
+ "switch" => return Some ( TokenType :: Switch ) ,
755
+ "static" => return Some ( TokenType :: Static ) ,
756
+ "typeof" => return Some ( TokenType :: TypeOf ) ,
757
+ "public" => return Some ( TokenType :: Public ) ,
758
+ _ => { }
759
+ }
760
+ }
761
+ 8 => {
762
+ // Common 8-letter keywords: function, continue
763
+ match word {
764
+ "function" => return Some ( TokenType :: Function ) ,
765
+ "continue" => return Some ( TokenType :: Continue ) ,
766
+ "debugger" => return Some ( TokenType :: Debugger ) ,
767
+ "abstract" => return Some ( TokenType :: Abstract ) ,
768
+ "interface" => return Some ( TokenType :: Interface ) ,
769
+ _ => { }
770
+ }
771
+ }
772
+ _ => { }
773
+ }
774
+
775
+ // Fallback to the PHF map for less common keywords
692
776
KEYWORDS . get ( word) . copied ( )
693
777
}
694
778
0 commit comments