Skip to content

Use CreateDataProperty instead of assignment in array iteration methods #1497

@arcde40

Description

@arcde40

Hi, I'm a student studying ECMAScript and Test262.

While testing, I noticed that some Array.prototype methods are failing specific Test262 cases. For example, Array.prototype.map fails about 5%(22/429) of the test cases.

Problem

According to a Spec, Array.prototype.map should use CreateDataPropertyOrThrow() to define values.
However, the current implementation is using simple assignment(using setter).

// internals/array-iteration.js
//...
for (;length > index; index++) if (NO_HOLES || index in self) {
  value = self[index]; 
  result = boundFunction(value, index, O);
  if (TYPE) {
	if (IS_MAP) target[index] = result; // Should be CreateDataPropertyOrThrow()
	else if (result) switch (TYPE) {
		// ...

When I replaced the assignment with createProperty() from internals/create_property.js, it passed most of the previously failing tests.

Performace bench

Since CreateDataProperty is a heavy function compared to simple assignment, I've ran some simple benchmarks to compare the performance:

Environment: Node v24.11.0 / MacBook Pro (M1 Pro)

Array Length Method Mean Time Difference
10 Current 174.42 ms -
createProperty() 173.83 ms Very small
100 Current 177.61 ms -
createProperty() 177.16 ms Very small
1,000,000 Current 390.81 ms -
createProperty() 613.39 ms ~1.57x slower

Although there's a performance drop for extreme cases (1M length), the overhead is negligible for common array sizes.

Conclusion

This issue seems to affect other methods as well, such as filter, flat and flatMap.

Is the current implementation intentional(for performance)? Considering the benchmark results, I believe prioritizing spec compliance is the right direction since there's no significant performance issue in real-world scenarios.

And if you agree, could I submit a PR to resolve this issue? (This would be my first contribution to open source, so I would appreciate your guidance!)

Thank you for your hard work on this project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions