Member-only story
10 JavaScript Array Methods To Boost Your Code Performance
Every, some, includes, and other array methods
With lots of instance methods, the Array API is one of the biggest in JavaScript. At first, it can be overwhelming. It is important to invest some time in getting familiar with it. Methods like map()
and forEach()
are widely abused. Instead, by using the right interface you can make your code faster, more efficient, and more readable.
There is something to consider when using array methods: some are immutable and some are mutable. Being aware of what type of method you are using is crucial. Misuse can lead to nasty bugs.
In this article, we will be looking at the ten more useful instance methods of the JavaScript Array object and where they shine.
1. Slice
If you want to make a copy of an array by a start and end index, you might want to use slice()
. This method is a pure function so it won’t have any side effects, nor mutate the target array.
“The
slice()
method returns a shallow copy of a portion of an array into a new array object selected fromstart
toend
(end
not included) wherestart
andend
represent the index of items in that array. The original array will not be modified.” — MDN