Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils.flatten is prone to stack overflow #222

Open
evil-shrike opened this issue Nov 29, 2023 · 0 comments
Open

utils.flatten is prone to stack overflow #222

evil-shrike opened this issue Nov 29, 2023 · 0 comments

Comments

@evil-shrike
Copy link

Givenflatten function from utils.js:

const flatten = (array) => {
    return [].concat(...array);
};

The problem with the current implementation is that it's prone to stack overflow ("Maximum call stack size exceeded") as the spread operator puts all arguments on stack.

I'd suggest changing it to a less fancy but more stable implementation:

const flatten = (array) => {
  let dest = [];
  const len = array.length
  for(let i = 0; i < len; i++){
      dest.push(...array[i])
  }
  return dest;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant