JavaScript performance library

Quick explanation of JavaScript Array methods:

array.concat
array.join
array.push
array.pop
array.shift
array.unshift
array.slice
array.splice
array.reverse
array.sort

Array.concat

array.concat(item1, item2, item3…)

Returns a new array built by joining the array and items. If an item is an array, every element of that array is added one by one.

Array on which concat method is executed remains unmodified.

var a1 = ["a","b","c"];
var a2 = ["d","e"];

// Array.concat with arrays
var a3 = a1.concat(a2);
console.log(a3); // =  ["a", "b", "c", "d", "e"]
console.log(a1); // =  ["a", "b", "c"]; array a1 is untouched

// Array.concat with other types
var a4 = a1.concat(NaN, function(){}, Infinity);
console.log(a4); // =  ["a", "b", "c", NaN, function(), Infinity]

 

Array.join

array.join(separator)

Returns a string with elements of the array separated with separator.

var a = ["a", "b", "c", "d", "e"];

console.log(a.join("; ")); // = "a; b; c; d; e"
console.log(a.join()); // = "a,b,c,d,e"; comma is the default separator

 

Array.push

array.push(item1, item2, item3…)

Adds an item on the end of the array.

Makes an array to work like a stack (LIFO – last in, first out).

var stack = ["a","b","c"];

console.log(stack.push("d","e")); // = 5; returns new length of the array
console.log(stack); // = ["a", "b", "c", "d", "e"]

 

Array.pop

array.pop()

Removes and returns an element on the end of the array.

Makes an array to work like a stack (LIFO – last in, first out).

var stack = ["a", "b", "c", "d", "e"];

console.log(stack.pop()); // = "e"; returns removed element
console.log(stack); // =  ["a", "b", "c", "d"]

 

Array.shift

array.shift()

Removes and returns an element from the beginning of the array.

Makes an array to work like a queue (FIFO – first in, first out).

var queue = ["a", "b", "c", "d", "e"];

console.log(queue.shift()); // = "a"
console.log(queue); // = ["b", "c", "d", "e"]

 

Array.unshift

array.unshift(item1, item2, item3…)

Adds an item on the beginning of the array.

Makes an array to work like a queue (FIFO – first in, first out).

var queue = ["a", "b", "c", "d", "e"];

console.log(queue.unshift("x", "z")); // = 7; new length of array returned
console.log(queue); // = ["x", "z", "a", "b", "c", "d", "e"]

 

Array.slice

array.slice(start, end)

Creates a new array from portion of array.

start defines the position from which elements from array are being copied, end marks where it should stop.

end by default is equal to array.length, thus entire array starting from start position is copied.

var myArray = ["a", "b", "c", "d", "e", "f"];

console.log(myArray.slice(2,4)); // ["c", "d"]
console.log(myArray.slice(2));   // ["c", "d", "e", "f"]
console.log(myArray.slice(-2));  // ["e", "f"]

 

 Array.splice

array.splice(start, amount, item1…)

Deletes amount of elements in array , beginning from position start. If additional parameters are provided, then the removed elements get replaced with the additional parameters.

The method returns an array of deleted elements.

var myArray = ["a", "b", "c", "d", "e", "f"];

myArray.splice(1, 2); // = ["b", "c"]
console.log(myArray); // =  ["a", "d", "e", "f"]

myArray.splice(2, 2, "ear", "fur"); // = ["e", "f"]
console.log(myArray); // =  ["a", "d", "ear", "fur"]

 

Array.reverse

array.reverse()

Returns an array in reversed order.

var myArray = ["a", "b", "c", "d", "e", "f"];

console.log(myArray.reverse()); // = ["f", "e", "d", "c", "b", "a"]

 

Array.sort

array.sort(compareFunction)

When no parameters are provided, the method sorts the array in ascending order. The extremely important thing to remember is that the sort method sorts the elements as strings. Thus:

var stringsArray = ["andrew", "joseph", "heinrich", "johanna", "adam"];
console.log(stringsArray.sort()); // = ["adam", "andrew", "heinrich", "johanna", "joseph"]

var intsArray = [5, 3, 43, 34, 14, 2, 21];
console.log(intsArray.sort()); // = [14, 2, 21, 3, 34, 43, 5]  !!!!!!

We can provide our own comparison function to perform sorting using our rules.

The comparison function takes two parameters and must return one of three numeric values:  a negative value, zero, or positive value.

Use negative value to mark that the first parameter is lesser.
Use zero value to mark that the parameters are equal.
Use positive value to mark that the right parameter is lesser.

Example usage of sort method:

var intsArray = [5, 3, 43, 34, 14, 2, 21];

intsArray.sort(function(a,b) {
    return a-b;
});

console.log(intsArray); // = [2, 3, 5, 14, 21, 34, 43]