Skip to content

Commit

Permalink
perf: Improve FormData
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Feb 8, 2024
1 parent 14e6a8f commit f07b204
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ class FormData {

// The delete(name) method steps are to remove all entries whose name
// is name from this’s entry list.
this[kState] = this[kState].filter(entry => entry.name !== name)
const state = this[kState]
for (let i = 0; i < state.length; ++i) {
if (state[i].name === name) {
// faster than Array#splice(i, 1)
for (let index = i; ++index < state.length;) {
state[index - 1] = state[index]
}
state.pop()
--i
}
}
}

get (name) {
Expand All @@ -74,14 +84,15 @@ class FormData {

// 1. If there is no entry whose name is name in this’s entry list,
// then return null.
const idx = this[kState].findIndex((entry) => entry.name === name)
if (idx === -1) {
return null
}

// 2. Return the value of the first entry whose name is name from
// this’s entry list.
return this[kState][idx].value
const state = this[kState]
for (let i = 0; i < state.length; ++i) {
if (state[i].name === name) {
return state[i].value
}
}
return null
}

getAll (name) {
Expand All @@ -95,9 +106,14 @@ class FormData {
// then return the empty list.
// 2. Return the values of all entries whose name is name, in order,
// from this’s entry list.
return this[kState]
.filter((entry) => entry.name === name)
.map((entry) => entry.value)
const state = this[kState]
const result = []
for (let i = 0; i < state.length; ++i) {
if (state[i].name === name) {
result.push(state[i].value)
}
}
return result
}

has (name) {
Expand All @@ -109,7 +125,13 @@ class FormData {

// The has(name) method steps are to return true if there is an entry
// whose name is name in this’s entry list; otherwise false.
return this[kState].findIndex((entry) => entry.name === name) !== -1
const state = this[kState]
for (let i = 0; i < state.length; ++i) {
if (state[i].name === name) {
return true
}
}
return false
}

set (name, value, filename = undefined) {
Expand Down Expand Up @@ -142,16 +164,26 @@ class FormData {

// 3. If there are entries in this’s entry list whose name is name, then
// replace the first such entry with entry and remove the others.
const idx = this[kState].findIndex((entry) => entry.name === name)
if (idx !== -1) {
this[kState] = [
...this[kState].slice(0, idx),
entry,
...this[kState].slice(idx + 1).filter((entry) => entry.name !== name)
]
} else {
const state = this[kState]
let found = false
for (let i = 0; i < state.length; ++i) {
if (state[i].name === name) {
if (!found) {
state[i] = entry
found = true
} else {
// faster than Array#splice(i, 1)
for (let index = i; ++index < state.length;) {
state[index - 1] = state[index]
}
state.pop()
--i
}
}
}
if (!found) {
// 4. Otherwise, append entry to this’s entry list.
this[kState].push(entry)
state.push(entry)
}
}

Expand Down

0 comments on commit f07b204

Please sign in to comment.