- Published on
- Harendra Kumar Kanojiya
1. Use array to resize or empty an
When we wish to enlarge an array, we just overwrite the length of the array:
let array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log(array.length); // returns the length as 10 array.length = 4; console.log(array.length); // returns the length as 4 console.log(array); // returns ['a', 'b', 'c', 'd']
2. Flatten the multidimensional array
The spread operator can be used to flatten a multidimensional array.
var entries = [1, [2, 5], [6, 7], 9]; var flat_entries = .concat(...entries); // [1, 2, 5, 6, 7, 9]
3. Obtaining the array's last item
Another clever trick is to use a negative number as the parameter to extract the final entry from the array.
var array = [1, 2, 3, 4, 5, 6]; console.log(array.slice(-1)); //  console.log(array.slice(-2)); // [5,6] console.log(array.slice(-3)); // [4,5,6]
4. Swap two variables without swapping the third.
Using the third variable, we can simply swap two variables. Using an extra variable, however, necessitates the use of more memory. We may also optimise memory with simply two variables.
let x = 1; let y = 2; [x, y] = [y, x]; // x = 2, y = 1
5. Memory-optimized merging of two arrays
If we need to combine two arrays, we may use the array.concat() function to accomplish it quickly.
var array1 = [1, 2, 3]; var array2 = [4, 5, 6]; console.log(array1.concat(array2)); // [1,2,3,4,5,6];
For tiny arrays, this is the best option. However, if we need to combine big arrays, this method is ineffective. Why is this the case? The reason for this is that when using the
array.concat() method on huge arrays, the resultant merged array will require a lot of memory.
In this case, the
array.push.apply(array1, array2) function may be used to merge the second array into the first, eliminating the need for additional memory. We can cut down on memory use this way.
var array1 = [1, 2, 3]; var array2 = [4, 5, 6]; console.log(array1.push.apply(array1, array2)); // [1,2,3,4,5,6];
6. Use splice instead of delete.
Splicing instead of deleting is a smart practise since it eliminates null/undefined space from your code.
Array = ["a", "b", "c", "d"] Array.splice(0, 2) ["a", "b"] Result: myArray ["c", "d"]
7. Instead of if/else, we should use a switch...case.
The switch...case is consistently underappreciated. Although switch...case and if/else accomplish almost identical tasks, the switch...case statement executes quicker than the if/else statement! What is the reason behind this? In the if/else statement, you must compare on average to reach the proper clause; however, the switch statement is essentially a lookup table with known alternatives.
8. As soon as feasible, break the loop.
Look for situations when completing every iteration in a loop isn't essential. For example, if we are looking for a specific value and discover it, we will not need to repeat the process. As a result, we need use a break statement to end the loop's execution:
9. Minimize the risk of memory leaks
10. Recognize the Big O notation