Initializing 2D and 3D array in JavaScript

Viewed 71

I am initializing my 2D array using the below snippet:

let arr = new Array(m).fill().map(() => new Array(n).fill(-1));

There are some other ways too to initialize it what I need help is in language like C++ there is a simpler way to initialize it using memset. How is time complexity and space complexity affected compared to C++ or Java when using this snippet?

1 Answers

In c++, with memset the time complexity is O(log n). however that if you're going to do any future modifications to the object, It would cost O(n).

Related