Introduction to the

Sparse array, array performance, dense array, traversal

In addition to programmers going bald, arrays also go bald. What, you don’t believe me? Let me prove it to you:

let arr = [0.1.2.3.4.5.6.7.8.9]

delete arr[2]

// Output [0, 1, empty, 3, 4, 5, 6, 7, 8, 9]
console.log(arr)

// Output 0 1 3 4 5 6 7 8 9
arr.forEach(i= > console.log(i))
Copy the code

When the third hair (ARr [2]) of the delete arR is removed, an empty follicle appears directly, which is not bald. ForEach the hair, not counting the empty follicles, so you believe that the array will also bald, so let’s take a good look at it.

The class name

Go to school the most common scene is the teacher roll call, according to the student number continuous read out the name of the students and tick in the record book, the speed is quite fast. Turn around and call a random student’s name and put it in the notepad, and you have to find the name in the notepad and tick it off, which is pretty slow.

In the above two scenarios, one uses an index and the other uses a hash, and a similar phenomenon exists in Array.

Array.prototype.unshift(); array.prototype.unshift (); array.prototype.unshift (); array.prototype.pop (); array.prototype.unshift (); This Array is called a dense Array.

However, if an Array performs non-contiguous Array operations such as delete arr[2], assign values beyond the current Array length, and assign values to non-contiguous indexes to operate on the Array, the continuity of the space is destroyed and the only way to locate the Array is through hash, thus the traversal will be slow. This Array is called a sparse Array, and you can think of it as being objectified to make it more like an Object.

Sparse arrays are generally slower and more memory intensive to implement than dense arrays, and finding elements in such arrays becomes as performance-deprived as finding regular objects. So when we’re using Array, we’re trying to avoid breaking its continuity, and we’re trying to manipulate it in array-related ways.

View on the console

In fact, in the console, it is very obvious whether the index of dense and sparse arrays is contiguous:

Sparse arrays convert dense arrays

usingArray.prototype.apply
// Do something to break the continuity
let arr = [1.3.4];
delete arr[3];
arr['a'] = 5;

// Print [1, empty, 3, empty, a: 5]
console.log(arr);

// Perform the conversion
arr = Array.apply(null, arr);

// output [1, undefined, 3, undefined]
console.log(arr);
Copy the code
usingArray.prototype.from
// Do something to break the continuity
let arr = [1.3.4];
delete arr[3];
arr['a'] = 5;

// Print [1, empty, 3, empty, a: 5]
console.log(arr);

// Perform the conversion
arr = Array.from(arr);

// output [1, undefined, 3, undefined]
console.log(arr);
Copy the code
Use the spread operator
// Do something to break the continuity
let arr = [1.3.4];
delete arr[3];
arr['a'] = 5;

// Print [1, empty, 3, empty, a: 5]
console.log(arr);

// Perform the conversion
arr = [...arr];

// output [1, undefined, 3, undefined]
console.log(arr);
Copy the code
Pay attention tonew Array

Note that the new Array only generates a pointer to a contiguous space, which is also discontiguous:

let arr = new Array(4);

// output [empty × 4]
console.log(arr);
Copy the code

Passing in null parameters

Call function with null, undefined

When calling a function, we sometimes need to skip some arguments by using null and undefined:

method('a'.null.'c');
method('a'.undefined.'c');
Copy the code

You can also use sparse arrays:

method(... ['a'.'c']);
Copy the code

Grow up together

In the confused city, there is always a partner to grow up together.

  • You can click on this if you want more people to see the articlegive a like.
  • If you want to inspire your mistress thereGithubGive aLittle stars.
  • If you want to communicate more with small two add wechatm353839115.

PushMeTop originally contributed to this article