lodash source code analysis - slice function

I recently learned the source code of Lodash in my spare time, and I am going to write a topic to share with you. This is the first article: slice function.

The role of the slice function is to create a cropped array from the position of start to end, but not including the position of end itself. At this time, I don't know if you have the same question as me, why not just use the slice method of the native array instead of rewriting it yourself? The author replied Enter image description

It probably means that he doesn't think there is much difference in performance, and the performance of Array#slice and baseSlice depends on the size of the array. BaseSlice is used here because they want to treat all data as dense, while Array#slice will deal with sparse arrays.

Looking at the source code, it's easy to do

function slice(array, start, end) {
  #1
  let length = array == null ? 0 : array.length
  if (!length) {
    return []
  }
  start = start == null ? 0 : start
  end = end === undefined ? length : end

  #2
  if (start < 0) {
    start = -start > length ? 0 : (length + start)
  }
  end = end > length ? length : end
  if (end < 0) {
    end += length
  }
  #3
  length = start > end ? 0 : ((end - start) >>> 0)
  start >>>= 0
  #4
  let index = -1
  const result = new Array(length)
  while (++index < length) {
    result[index] = array[index + start]
  }
  return result
}

Learning highlights

Here, I want you and me to think about every little question and think about it together

1. What is the difference between null and undefined?

The original version of JavaScript differentiated like this: null is an object representing "nothing", which is 0 when converted to a number; undefined is a primitive value representing "nothing", which is NaN when converted to a number.

Number(null);//0
Number(undefined);//NaN

However, such a distinction was quickly shown to be infeasible in practice. Currently, null and undefined are basically synonymous, with only some minor differences.

null means "no object", i.e. there should be no value here

  • 1) As a parameter of a function, it means that the parameter of the function is not an object
  • 2) As the end point of the object prototype chain

undefined means "missing value", i.e. there should be a value here, but it hasn't been defined

  • 1) The variable is declared, but when it is not assigned a value, it is equal to undefined
  • 2) When the function is called, the parameter that should be provided is not provided, and the parameter is equal to undefined
  • 3) The object has no assigned property, and the value of the property is undefined
  • 4) When the function has no return value, it returns undefined by default

2. What is the function of num >>> 0?

First of all, we need to understand that the function of >>> is to turn a number [value of any type] into an unsigned 32-bit integer;

The function of num >>> 0 is to turn num into an unsigned 32-bit integer, regardless of whether num is a negative number or a decimal. And we also need to know that the maximum length of JavaScript arrays is 2^32-1, so doing this also avoids the index of the array going out of bounds.

Source code analysis

First, the function receives three parameters, the first parameter is the array to be trimmed, and the second and third are optional parameters, which are the start position and end position respectively. Returns the cropped array

The following steps are explained [explain the location of the corresponding source code mark]

#1 Use the ternary operator to judge whether an array is passed in. If not, set the length of the array to 0, otherwise set it to the length of the array, and then judge whether the length of the array is 0. If it is 0, return an empty array; then, these two Optional parameters also use the ternary operator to determine whether the incoming value is passed in, and the incoming value is taken, otherwise the default value is taken. Here is a question: start and end have the same properties, why use null and undefined respectively when judging?

#2 Determine whether the parameter start is a negative number. If start is a negative number, compare the opposite number of start with the size of the array length. If it is greater than the length of the array, then assign the value to 0; otherwise, assign start to length + start, that is, start Position; also judge whether the parameter end is greater than the length of the array, if it is greater than the length of the array, then assign it as the length of the array, and then judge whether the end is less than 0, if it is less than 0, assign it as end + length, that is, the end position

#3 The start position and end position can be determined by  #2 judging whether start is greater than end. If it is greater than end, set the value of length to 0. Otherwise, subtract start from end and move zero bits to the right without sign [about >>>0 If you don't understand the function, you can go to the top to see] and then move start to the right unsigned zero.

#4 A new array is created, and the value of the array we want to get is copied from the original array using the loop index method; the final cropped array.

Well, I have finished all I know here. If you want to see the analysis of each sentence of code, you can follow me . If there is any mistake, please point out the mistakes, and the little sister will correct it immediately. Interested can follow me

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325091844&siteId=291194637