Skip to content

Commit

Permalink
perf: always use the same prototype Iterator (nodejs#2743)
Browse files Browse the repository at this point in the history
* perf: always use the same prototype Iterator

* add iteratorMixin

* rename state to object

* fixup

* perf: use class make faster

* fixup

* fixup

* fixup

* fixup

* fixup

* add test

* fix test name

* simplify
  • Loading branch information
tsctx authored and crysmags committed Feb 21, 2024
1 parent 1427f26 commit 9e1b352
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 160 deletions.
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

0 comments on commit 9e1b352

Please sign in to comment.