| by Arround The Web | No comments

Is JavaScript slice() Method Slow?

The JavaScript “slice()” method extracts a portion of an array and generates a new array that contains the required elements. The “slice()” method requires only the initial and ending indexes of an array to obtain a new array with the elements residing in the indexes range. This makes the “slice()” method easier to use and as it creates a new array each time without modifying the old or original array.

This blog will discuss whether the slice() method is slow or not.

Is the JavaScript slice() Method Slow?

The slice() method is generally two times slower than manual copying of data elements from one array to another. Because the time taken to perform the slicing operation grows proportional to the number of elements being sliced. If there is a small data set that is going to be sliced then, the “slice()” method can be used but when dealing with large datasets, there can be performance concerns. However, the actual performance of the “slice()” method is dependent on the JavaScript engine and the part of the array being sliced.

In scenarios, where the performance factor is very important and you are dealing with large datasets, it is better not to use the “slice()” method. With larger datasets, users can utilize manual indexing techniques or “for” loops, especially when there is no need to create a new array of data. If there is a need to use a built-in method, then try to use the “Array.prototype.map()” or “Array.prototype.filter()” due to their very less overhead while the creation of a new array.

Time Complexity of slice() Method

The “slice()” method has a time complexity of “O(n)”, where “n” is the count of all elements being extracted from the original array. In simple terms, you can say the “slice()” method has a linear time complexity because it is directly proportional to the number of selected elements. If the data set is small means if there are less number of elements that are being sliced then, the time complexity decreases and vice versa in the case of the large data set. Let’s walk through some use cases that are mentioned below:

  • Time complexity: O(1) the time complexity for a single constant element.
  • Time complexity: O(n) where “n” is the small data set number of elements you want to extract.
  • Time complexity: O(l) where “l” is the total number of large data set elements in the original array.

Syntax

The syntax for the slice() method in JavaScript is shown below:

currentArr.slice(initial, ending)

The above syntax of the “slice()” method consists of two parameters: “initial” and “ending“. The “initial” parameter specifies the index from which the array element gets extracted, and the “ending” parameter specifies the array index till the extracting happens. By default, the “initial” parameter is set to index “0”. If the “ending” parameter is not provided, the slice extracts elements up to the last element residing in the array.

Example: Using “slice()” to Select the Specific Range of Data Elements

Let’s have a simple example in which the elements residing in the specified index range get selected and placed in a newly created array using the “slice()” method:

<body>
 <h3> Linuxhint Article </h3>
 <h5>Friendly Territories:</h5>
 <p id="target"> </p>

 <script>
   const country = ["Austria", "Turkey", "UAE", "Qatar", "Korea"];
   const friend = country.slice(1, 4);

   document.getElementById('target').innerHTML = friend;
 </script>
</body>

In the above code:

  • First, the array “country” is created that contains several country’s names.
  • Next, the “slice()” method is applied to the “country” array having the parameters of “1” and “4”.
  • This method selects the residing element from index “1” to “4” but the element at index “4” does not get selected.
  • The new array is stored in a new array named “friend”. This new array then gets displayed on the web page over an HTML element having an “id” of “target”.

After the compilation:

The output shows that data elements residing inside the range have been selected and stored in the new array.

Conclusion

The “slice()” method is slow when the provided data sets are enormously large because the output of the slice() method is directly proportional to the number of selected data. It works perfectly on small and average data sets, but when working with large data sets, the manual copy of data elements or other methods should be used. This blog has answered the query related to the slowness of the “slice()” method.

Share Button

Source: linuxhint.com

Leave a Reply