1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:57:45 +00:00

LibJS: Let Array.prototype.map() resize new array before loop

Currently we would create an empty array of size 0 and appening results
of the callback function while skipping empty values.

This is incorrect, we should be initializing a full array of the correct
size beforehand and then inserting the results while still skipping
empty values.

Wrong: new Array(5).map(() => {}) // []
Right: new Array(5).map(() => {}) // [<empty> * 5]
This commit is contained in:
Linus Groh 2020-04-28 17:46:07 +01:00 committed by Andreas Kling
parent 0a0ba64383
commit ad8abce8a5
2 changed files with 15 additions and 1 deletions

View file

@ -26,6 +26,10 @@ try {
assert([1, 2, 3].map(callback).length === 3);
assert(callbackCalled === 3);
callbackCalled = 0;
assert([1, , , "foo", , undefined, , ,].map(callback).length === 8);
assert(callbackCalled === 3);
var results = [undefined, null, true, "foo", 42, {}].map((value, index) => "" + index + " -> " + value);
assert(results.length === 6);
assert(results[0] === "0 -> undefined");