Skip to content

Commit

Permalink
avoid usage intermediate array iterator in URLSearchParams iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 18, 2023
1 parent 92deeaf commit 037fced
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog
##### Unreleased
- Directly specified `type: commonjs` in `package.json` of all packages to avoid potential breakage in future Node versions, see [this issue](https://github.com/nodejs/TSC/issues/1445)
- Some minor internal fixes and optimizations
- Compat data improvements:
- [`String.prototype.{ isWellFormed, toWellFormed }`](https://github.com/tc39/proposal-is-usv-string) marked as [supported from FF119](https://bugzilla.mozilla.org/show_bug.cgi?id=1850755)
- Added React Native 0.73 Hermes compat data, mainly fixes of [some issues](https://github.com/facebook/hermes/issues/770)
Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/modules/es.array.iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ module.exports = defineIterator(Array, 'Array', function (iterated, kind) {
}, function () {
var state = getInternalState(this);
var target = state.target;
var kind = state.kind;
var index = state.index++;
if (!target || index >= target.length) {
state.target = undefined;
return createIterResultObject(undefined, true);
}
switch (kind) {
switch (state.kind) {
case 'keys': return createIterResultObject(index, false);
case 'values': return createIterResultObject(target[index], false);
} return createIterResultObject([index, target[index]], false);
Expand Down
29 changes: 19 additions & 10 deletions packages/core-js/modules/web.url-search-params.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var create = require('../internals/object-create');
var createPropertyDescriptor = require('../internals/create-property-descriptor');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var createIterResultObject = require('../internals/create-iter-result-object');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var wellKnownSymbol = require('../internals/well-known-symbol');
var arraySort = require('../internals/array-sort');
Expand Down Expand Up @@ -113,17 +114,23 @@ var serialize = function (it) {
var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
setInternalState(this, {
type: URL_SEARCH_PARAMS_ITERATOR,
iterator: getIterator(getInternalParamsState(params).entries),
target: getInternalParamsState(params).entries,
index: 0,
kind: kind
});
}, 'Iterator', function next() {
var state = getInternalIteratorState(this);
var kind = state.kind;
var step = state.iterator.next();
var entry = step.value;
if (!step.done) {
step.value = kind === 'keys' ? entry.key : kind === 'values' ? entry.value : [entry.key, entry.value];
} return step;
var target = state.target;
var index = state.index++;
if (!target || index >= target.length) {
state.target = undefined;
return createIterResultObject(undefined, true);
}
var entry = target[index];
switch (state.kind) {
case 'keys': return createIterResultObject(entry.key, false);
case 'values': return createIterResultObject(entry.value, false);
} return createIterResultObject([entry.key, entry.value], false);
}, true);

var URLSearchParamsState = function (init) {
Expand All @@ -143,6 +150,7 @@ URLSearchParamsState.prototype = {
this.update();
},
parseObject: function (object) {
var entries = this.entries;
var iteratorMethod = getIteratorMethod(object);
var iterator, next, step, entryIterator, entryNext, first, second;

Expand All @@ -157,22 +165,23 @@ URLSearchParamsState.prototype = {
(second = call(entryNext, entryIterator)).done ||
!call(entryNext, entryIterator).done
) throw new TypeError('Expected sequence with length 2');
push(this.entries, { key: $toString(first.value), value: $toString(second.value) });
push(entries, { key: $toString(first.value), value: $toString(second.value) });
}
} else for (var key in object) if (hasOwn(object, key)) {
push(this.entries, { key: key, value: $toString(object[key]) });
push(entries, { key: key, value: $toString(object[key]) });
}
},
parseQuery: function (query) {
if (query) {
var entries = this.entries;
var attributes = split(query, '&');
var index = 0;
var attribute, entry;
while (index < attributes.length) {
attribute = attributes[index++];
if (attribute.length) {
entry = split(attribute, '=');
push(this.entries, {
push(entries, {
key: deserialize(shift(entry)),
value: deserialize(join(entry, '='))
});
Expand Down

0 comments on commit 037fced

Please sign in to comment.