Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spread syntax vs slice method

Tags:

I was trying to understand what is the difference between spread syntax vs slice method in the following approach.

suppose I want to make an actual copy of an array, I can probably easily do it using spread syntax

var fruits = ["Banana", "Chips" , "Orange", "Lemon", "Apple", "Mango"] var newCitrus = [...fruits] 

If I console.log this

["Banana", "Chips", "Orange", "Lemon", "Apple", "Mango"]  

but I can also create a copy of an array using the slice method. Considering the same array above, if I do something like this...

var citrus = fruits.slice(0); 

and then console log it, it will give me exactly the same array which I would've got through spread syntax

["Banana", "Chips", "Orange", "Lemon", "Apple", "Mango"]  

Since both of them takes about the same time to code/write, What is the difference here? which approach should I usually choose?

like image 385
iRohitBhatia Avatar asked Jul 03 '18 23:07

iRohitBhatia


People also ask

What is the spread syntax?

Spread syntax ( ... ) allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

What is one difference between the splice () and slice () methods?

The splice() method returns the removed items in an array. The slice() method returns the selected element(s) in an array, as a new array object. The splice() method changes the original array and slice() method doesn't change the original array.

What is the difference between Slice and split?

Slice() is used to extract elements from an array and return a new array, and it would not modify the origin array. Split() is used to convert the input string into an array by the separator, and since string is immutable, it would not modify the origin string.

What is the use of slice () method?

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array.


1 Answers

Performance aside slice is just a function on Array.prototype so it will work only for arrays. Spread syntax on the other hand will work for any iterable (object which satisfy iterable protocol) so it will work out of the box on any String, Array, TypedArray, Map and Set. You can also easily create custom iterables.

There is also a difference when it comes to sliceing and spreading arrays with holes (sparse arrays). As you can see below, slice will preserve sparseness while spread will fill holes with undefined.

Array(3)         // produces sparse array: [empty × 3] Array(3).slice() // produces [empty × 3] [...Array(3)]    // produces [undefined, undefined, undefined] 

Spread syntax can also be used to make shallow clones of objects:

const obj = { foo: 'bar', baz: 42 }; const clone = { ...obj }; obj.foo === clone.foo // true obj.baz === clone.baz // true obj === clone         // false (references are different) 
like image 171
Bartosz Gościński Avatar answered Oct 19 '22 03:10

Bartosz Gościński