WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -233,7 +232,7 @@ The native implementation should strictly avoid creating intermediate arrays or
233
232
2. Iterate directly over the object's own property descriptors
234
233
- Access the internal property keys directly via the object's internal slots.
235
234
- For each own property:
236
-
- Determine if the key is a numeric index, a regular non-index string, or a symbol.
235
+
- Determine if the key is a string or a symbol.
237
236
- Check if the property type matches any specified in `keyTypes`.
238
237
- If `enumerable` is not `'all'`, match the property's enumerability against the provided boolean value.
239
238
- If the property meets all criteria, increment the counter.
@@ -244,38 +243,40 @@ See the [spec proposal](./spec.emu) for details.
244
243
## Alternatives Considered
245
244
246
245
-**Multiple separate methods**: Rejected due to increased cognitive load and API complexity.
246
+
-**Using booleans for key types**: The default would have diverging boolean defaults for different key types (in other words, 4 possible combinations for only 3 possible states). Thus, an enum is considered a better approach.
247
247
248
248
## TC39 Stages and Champion
249
249
250
-
- Ready for **Stage 1** (proposal)
250
+
- Ready for **Stage 2**
251
251
252
252
## Use Cases
253
253
254
254
- Improved readability and explicit intent
255
-
- Significant performance gains
256
-
- Reduced memory overhead
257
-
- Simpler code
255
+
- Significant **performance** gains
256
+
-**Reduced memory** overhead
257
+
-**Simpler code**
258
258
259
259
## Precedent
260
260
261
261
Frequent patterns in widely-used JavaScript runtimes, frameworks, and libraries (Node.js, React, Angular, Lodash) demonstrate the common need for an optimized property counting mechanism.
262
262
263
+
The regular expression exec/match/matchAll methods produce a "match object" that is an Array, with non-index string properties on it (lastIndex, groups, etc).
264
+
263
265
## Polyfill
264
266
265
267
```js
266
-
// NOTE: do not use this polyfill in a production environment
@@ -319,6 +312,7 @@ Object.propertyCount = function (target, options) {
319
312
-**Performance**: Native implementation will significantly outperform existing approaches by eliminating intermediate arrays.
320
313
-**Flexibility**: Enumerable properties counted by default; easy inclusion/exclusion.
321
314
-**Simplicity**: Improved code readability and clarity.
315
+
-**Future proofing**: The second argument is an options object and this potentially allows future additions (e.g., to include inherited properties, or only writable properties, etc).
<p>When the `Object.propertyCount` method is called, the following steps are taken:</p>
24
-
<emu-alg>
25
-
1. If _target_ is not an Object, throw a TypeError exception.
26
-
1. Let _resolvedOptions_ be ? GetOptionsObject(_options_).
27
-
1. Let _keyTypes_ be CreateArrayFromList(« *"index"*, *"nonIndexString"* »).
28
-
1. Let _keyTypesOption_ be ? Get(_resolvedOptions_, *"keyTypes"*).
29
-
1. If _keyTypesOption_ is not *undefined*, then
30
-
1. If _keyTypesOption_ is not an Object, throw a TypeError exception.
31
-
1. Set _keyTypes_ to ? CreateListFromArrayLike(_keyTypesOption_).
32
-
1. If _keyTypes_ contains any value other than *"index"*, *"nonIndexString"*, or *"symbol"*, or if any of those values are repeated, throw a TypeError exception.
33
-
1. Let _enumerable_ be ? Get(_resolvedOptions_, *"enumerable"*).
34
-
1. If _enumerable_ is *undefined*, set _enumerable_ to *true*.
35
-
1. If _enumerable_ is not one of *true*, *false*, or *"all"*, throw a TypeError exception.
36
-
1. Let _count_ be 0.
37
-
1. Let _ownKeys_ be _target_.[[OwnPropertyKeys]]().
38
-
1. For each element _key_ of _ownKeys_, do
39
-
1. Let _desc_ be ? _target_.[[GetOwnProperty]](_key_).
40
-
1. If _desc_ is not *undefined*, and either _enumerable_ is *"all"* or _enumerable_ is _desc_.[[Enumerable]], then
41
-
1. If _key_ is a Symbol and _keyTypes_ contains *"symbol"*, increment _count_ by 1.
42
-
1. Else if _key_ is an array index and _keyTypes_ contains *"index"*, increment _count_ by 1.
43
-
1. Else if _keyTypes_ contains *"nonIndexString"*, increment _count_ by 1.
0 commit comments