Skip to content

Commit c9d079f

Browse files
authored
perf: avoid deoptimization on out of bounds (#4)
1 parent 1544f13 commit c9d079f

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

lib/parse.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ function parse(input) {
3030
let equalityIndex = -1;
3131
let shouldDecode = false;
3232
let hasPlus = false;
33+
const lastIndex = input.length;
3334

3435
// Have a boundary of input.length + 1 to access last pair inside the loop.
35-
for (let i = 0; i < input.length + 1; i++) {
36-
let c = input.charCodeAt(i);
36+
for (let i = 0; i < lastIndex + 1; i++) {
37+
let c = (i !== lastIndex && input.charCodeAt(i)) || 0;
3738

3839
// Handle '&' and end of line to pass the current values to result
39-
if (c === 38 || isNaN(c)) {
40+
if (c === 38 || c === 0) {
4041
// Check if the current range consist of a single key
4142
if (equalityIndex <= startingIndex) {
4243
key = input.slice(startingIndex + 1, i);

0 commit comments

Comments
 (0)