Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 382ea5b

Browse files
authored
Even faster array parsing (#20)
1 parent 799ee89 commit 382ea5b

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

index.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const NULL_STRING = 'NULL'
2020
function makeParseArrayWithTransform (transform) {
2121
const haveTransform = transform != null
2222
return function parseArray (str) {
23+
const rbraceIndex = str.length - 1
24+
if (rbraceIndex === 1) {
25+
return []
26+
}
27+
if (str[rbraceIndex] !== RBRACE) {
28+
throw new Error('Invalid array text - must end with }')
29+
}
30+
2331
// If starts with `[`, it is specifying the index boundas. Skip past first `=`.
2432
let position = 0
2533
if (str[position] === LBRACKET) {
@@ -29,17 +37,12 @@ function makeParseArrayWithTransform (transform) {
2937
if (str[position++] !== LBRACE) {
3038
throw new Error('Invalid array text - must start with {')
3139
}
32-
const rbraceIndex = str.length - 1
33-
if (str[rbraceIndex] !== RBRACE) {
34-
throw new Error('Invalid array text - must end with }')
35-
}
3640
const output = []
3741
let current = output
3842
const stack = []
3943

4044
let currentStringStart = position
41-
const currentStringParts = []
42-
let hasStringParts = false
45+
let currentString = ''
4346
let expectValue = true
4447

4548
for (; position < rbraceIndex; ++position) {
@@ -56,8 +59,7 @@ function makeParseArrayWithTransform (transform) {
5659
while (backSlash !== -1 && backSlash < dquot) {
5760
position = backSlash
5861
const part = str.slice(currentStringStart, position)
59-
currentStringParts.push(part)
60-
hasStringParts = true
62+
currentString += part
6163
currentStringStart = ++position
6264
if (dquot === position++) {
6365
// This was an escaped doublequote; find the next one!
@@ -68,14 +70,9 @@ function makeParseArrayWithTransform (transform) {
6870
}
6971
position = dquot
7072
const part = str.slice(currentStringStart, position)
71-
if (hasStringParts) {
72-
const final = currentStringParts.join('') + part
73-
current.push(haveTransform ? transform(final) : final)
74-
currentStringParts.length = 0
75-
hasStringParts = false
76-
} else {
77-
current.push(haveTransform ? transform(part) : part)
78-
}
73+
currentString += part
74+
current.push(haveTransform ? transform(currentString) : currentString)
75+
currentString = ''
7976
expectValue = false
8077
} else if (char === LBRACE) {
8178
const newArray = []

0 commit comments

Comments
 (0)