## DEV Community is a community of 665,497 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Top 20 JavaScript tips and tricks to increase your Speed and Efficiency

Igor Snitkin

Looks more like “Hey, I know a bit of JS” rather than tips really. Let’s start with one, can you think of a way to make the matrix in a bit more efficient manner (hint: 2 iterations less), so it starts looking like a tip that will make me more efficient as advertised

Kapil Raghuwanshi🖥

Well thanks for the comment @snigo . And I didn't want to sound like “Hey, I know a bit of JS”, if I did, share the points/sentences, will try to fix them.
However, as I pointed out earlier in the post that - there can be more than one approaches to the same problem, so feel free to share yours here.

Igor Snitkin

Enjoy:

``````const matrix = (x, y = x, fillValue = 0) => Array.from({ length: y }, () => Array(x).fill(fillValue));
``````

Now, number two: what's going to be the sum of an empty array in your example?

Kapil Raghuwanshi🖥

``````const arr = Array.from(Array(4).fill('O'), () => new Array(4).fill('O'));
``````
``````const arr = [...Array(8)].map(() => Array(8).fill("0"))
``````
Igor Snitkin • Edited

If we talk about efficiency, then we first need to figure out `.map` or `.from`:

• `map`: maps over elements returning new array 👎
• `from`: mapFn maps over elements in place 👍

So `from`, which leaves us with the question what we're going to create our array from, right? So if we compare:

``````// Array from array
Array.from(Array(3), () => Array(3).fill(0));

// Array from object
Array.from({ length: 3 }, () => Array(3).fill(0));
``````

...it will boil down to the question what's more efficient to create `Array(3)` or `{ length: 3 }`, and given arrays in JS are just objects it really comes to the number of properties we need to create for the object. How many properties does `Array(3)` have? (hint: 4) How many properties does `{ length: 3 }` have? (hint: 1)

I hope this will clear things a bit