Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: always use the same prototype Iterator #2743

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 2 additions & 60 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { isBlobLike, makeIterator } = require('./util')
const { isBlobLike, iteratorMixin } = require('./util')
const { kState } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
Expand Down Expand Up @@ -154,62 +154,9 @@ class FormData {
this[kState].push(entry)
}
}

entries () {
webidl.brandCheck(this, FormData)

return makeIterator(
() => this[kState],
'FormData',
'key+value',
'name', 'value'
)
}

keys () {
webidl.brandCheck(this, FormData)

return makeIterator(
() => this[kState],
'FormData',
'key',
'name', 'value'
)
}

values () {
webidl.brandCheck(this, FormData)

return makeIterator(
() => this[kState],
'FormData',
'value',
'name', 'value'
)
}

/**
* @param {(value: string, key: string, self: FormData) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
webidl.brandCheck(this, FormData)

webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.forEach' })

if (typeof callbackFn !== 'function') {
throw new TypeError(
"Failed to execute 'forEach' on 'FormData': parameter 1 is not of type 'Function'."
)
}

for (const [key, value] of this) {
callbackFn.call(thisArg, value, key, this)
}
}
}

FormData.prototype[Symbol.iterator] = FormData.prototype.entries
iteratorMixin('FormData', FormData, kState, 'name', 'value')

Object.defineProperties(FormData.prototype, {
append: kEnumerableProperty,
Expand All @@ -218,11 +165,6 @@ Object.defineProperties(FormData.prototype, {
getAll: kEnumerableProperty,
has: kEnumerableProperty,
set: kEnumerableProperty,
entries: kEnumerableProperty,
keys: kEnumerableProperty,
values: kEnumerableProperty,
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false },
[Symbol.toStringTag]: {
value: 'FormData',
configurable: true
Expand Down
62 changes: 2 additions & 60 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { kHeadersList, kConstruct } = require('../core/symbols')
const { kGuard } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const {
makeIterator,
iteratorMixin,
isValidHeaderName,
isValidHeaderValue
} = require('./util')
Expand Down Expand Up @@ -504,67 +504,14 @@ class Headers {
return headers
}

keys () {
webidl.brandCheck(this, Headers)

return makeIterator(
() => this[kHeadersSortedMap],
'Headers',
'key',
0, 1
)
}

values () {
webidl.brandCheck(this, Headers)

return makeIterator(
() => this[kHeadersSortedMap],
'Headers',
'value',
0, 1
)
}

entries () {
webidl.brandCheck(this, Headers)

return makeIterator(
() => this[kHeadersSortedMap],
'Headers',
'key+value',
0, 1
)
}

/**
* @param {(value: string, key: string, self: Headers) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
webidl.brandCheck(this, Headers)

webidl.argumentLengthCheck(arguments, 1, { header: 'Headers.forEach' })

if (typeof callbackFn !== 'function') {
throw new TypeError(
"Failed to execute 'forEach' on 'Headers': parameter 1 is not of type 'Function'."
)
}

for (const [key, value] of this) {
callbackFn.call(thisArg, value, key, this)
}
}

[Symbol.for('nodejs.util.inspect.custom')] () {
webidl.brandCheck(this, Headers)

return this[kHeadersList]
}
}

Headers.prototype[Symbol.iterator] = Headers.prototype.entries
iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1)

Object.defineProperties(Headers.prototype, {
append: kEnumerableProperty,
Expand All @@ -573,11 +520,6 @@ Object.defineProperties(Headers.prototype, {
has: kEnumerableProperty,
set: kEnumerableProperty,
getSetCookie: kEnumerableProperty,
keys: kEnumerableProperty,
values: kEnumerableProperty,
entries: kEnumerableProperty,
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false },
[Symbol.toStringTag]: {
value: 'Headers',
configurable: true
Expand Down
Loading
Loading