Closed Bug 1616116 Opened 5 years ago Closed 5 years ago

Engine bug on Array.reduce

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: ktmd, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36

Steps to reproduce:

Please take a look at https://jsfiddle.net/vjq0786f/ and https://jsfiddle.net/cytpk0ah/

Actual results:

When JSON.stringify(obj1) on https://jsfiddle.net/vjq0786f/. It returns

{"A1":{"boxId":"A1","isSquare":true,"Q6":{"boxId":"Q6","isSquare":false,"C5":{"boxId":"C5","isSquare":true}}},"Q6":{"boxId":"Q6","isSquare":false,"C5":{"boxId":"C5","isSquare":true}},"C5":{"boxId":"C5","isSquare":true}}

Meanwhile, JSON.stringify(obj1) on https://jsfiddle.net/cytpk0ah/ returns
{"A1":{"boxId":"A1","isSquare":true},"Q6":{"boxId":"Q6","isSquare":false},"C5":{"boxId":"C5","isSquare":true}}

The above 2 jsfiddle are pretty much identical, except /vjq0786f have another independent function (no side effect).

Expected results:

Produces the same result

Can you provide the following information?

  1. which OS do you use to observe the issue?
  2. which browser and version do you use to observe the issue?
  3. if it's Firefox, does the issue happen on Safe mode[1] ?

[1] https://support.mozilla.org/en-US/kb/troubleshoot-firefox-issues-using-safe-mode

Component: Untriaged → JavaScript Engine
Flags: needinfo?(ktmd)
Product: Firefox → Core

Thanks for the bug report. I think this is a problem with your code (I see the same behavior in Safari).

The above 2 jsfiddle are pretty much identical, except /vjq0786f have another independent function (no side effect).

var obj2 = boxes.reduce((accumulator, currentValue) => {
  return accumulator[currentValue.boxId] = currentValue;
}, {});

This does have side-effects: the callback's return value (currentValue in this case) is used as accumulator so it is modified later on. It should be more like what you did with obj1:

var obj2 = boxes.reduce((accumulator, currentValue) => {
  accumulator[currentValue.boxId] = currentValue;
  return accumulator;
}, {});
Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → INVALID
Flags: needinfo?(ktmd)
You need to log in before you can comment on or make changes to this bug.