Skip to content

Commit

Permalink
perf(parse): avoid more allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk committed Jun 14, 2022
1 parent c4ca56b commit 649b6ec
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 65,13 @@ const tokenRef = {
const FIRST_DOUBLE_LENGTH_CODE = 2 ** 16
const getCodePointLength = c => c >= FIRST_DOUBLE_LENGTH_CODE ? 2 : 1

const LITERAL_ULL = [117 /* 'u' */, 108 /* 'l' */, 108 /* 'l' */]
const LITERAL_RUE = [114 /* 'r' */, 117 /* 'u' */, 101 /* 'e' */]
const LITERAL_ALSE = [97 /* 'a' */, 108 /* 'l' */, 115 /* 's' */, 101 /* 'e' */]
const LITERAL_NFINITY = [110 /* 'n' */, 102 /* 'f' */, 105 /* 'i' */, 110 /* 'n' */, 105 /* 'i' */, 116 /* 't' */, 121 /* 'y' */]
const LITERAL_AN = [97 /* 'a' */, 78 /* 'N' */]


let source
let parseState
let stack
Expand Down Expand Up @@ -293,17 300,17 @@ const lexStates = {

case 110 /* 'n' */:
skip()
literal([117 /* 'u' */, 108 /* 'l' */, 108 /* 'l' */])
literal(LITERAL_ULL)
return newToken(Token.null, null)

case 116 /* 't' */:
skip()
literal([114 /* 'r' */, 117 /* 'u' */, 101 /* 'e' */])
literal(LITERAL_RUE)
return newToken(Token.boolean, true)

case 102 /* 'f' */:
skip()
literal([97 /* 'a' */, 108 /* 'l' */, 115 /* 's' */, 101 /* 'e' */])
literal(LITERAL_ALSE)
return newToken(Token.boolean, false)

case 45 /* '-' */:
Expand Down Expand Up @@ -340,12 347,12 @@ const lexStates = {

case 73 /* 'I' */:
skip()
literal([110 /* 'n' */, 102 /* 'f' */, 105 /* 'i' */, 110 /* 'n' */, 105 /* 'i' */, 116 /* 't' */, 121 /* 'y' */])
literal(LITERAL_NFINITY)
return newToken(Token.numeric, Infinity)

case 78 /* 'N' */:
skip()
literal([97 /* 'a' */, 78 /* 'N' */])
literal(LITERAL_AN)
return newToken(Token.numeric, NaN)

case 34 /* '"' */:
Expand Down Expand Up @@ -459,12 466,12 @@ const lexStates = {

case 73 /* 'I' */:
skip()
literal([110 /* 'n' */, 102 /* 'f' */, 105 /* 'i' */, 110 /* 'n' */, 105 /* 'i' */, 116 /* 't' */, 121 /* 'y' */])
literal(LITERAL_NFINITY)
return newToken(Token.numeric, sign * Infinity)

case 78 /* 'N' */:
skip()
literal([97 /* 'a' */, 78 /* 'N' */])
literal(LITERAL_AN)
return newToken(Token.numeric, NaN)
}

Expand Down

0 comments on commit 649b6ec

Please sign in to comment.